手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

用Qt写的平均绩点计算器

[日期:2013-04-22] 来源:Linux社区  作者:nimingzhe [字体:大 中 小]

mainwindow,cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QList>
#include<QString>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    count=2;
    LX.append(ui->LEX1);
    LX.append(ui->LEX2);
    LJ.append(ui->LEJ1);
    LJ.append(ui->LEJ2);
    connect(ui->cal,SIGNAL(clicked()),this,SLOT(slot_cal()));
    connect(ui->add,SIGNAL(clicked()),this,SLOT(slot_add()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_add()
{
    count++;
    LX.append(new QLineEdit(this));
    LJ.append(new QLineEdit(this));
    ui->VLX->addWidget(LX[count-1]);
    ui->VLJ->addWidget(LJ[count-1]);

}

void MainWindow::slot_cal()
{
    double sum=0,sum2=0;
    for(int i=0;i<=count-1;i++)
        sum=sum+LX[i]->text().toInt();//zong xue fen
    for(int i=0;i<=count-1;i++)
        sum2=sum2+(LX[i]->text().toDouble())*(LJ[i]->text().toDouble());
    ui->pjjd->setText(QString::number(sum2/sum));
}

mainwindow,ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
  <rect>
    <x>0</x>
    <y>0</y>
    <width>520</width>
    <height>405</height>
  </rect>
  </property>
  <property name="palette">
  <palette>
    <active>
    <colorrole role="Base">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
      </color>
      </brush>
    </colorrole>
    <colorrole role="Window">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>132</red>
        <green>239</green>
        <blue>240</blue>
      </color>
      </brush>
    </colorrole>
    </active>
    <inactive>
    <colorrole role="Base">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>255</red>
        <green>255</green>
        <blue>255</blue>
      </color>
      </brush>
    </colorrole>
    <colorrole role="Window">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>132</red>
        <green>239</green>
        <blue>240</blue>
      </color>
      </brush>
    </colorrole>
    </inactive>
    <disabled>
    <colorrole role="Base">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>132</red>
        <green>239</green>
        <blue>240</blue>
      </color>
      </brush>
    </colorrole>
    <colorrole role="Window">
      <brush brushstyle="SolidPattern">
      <color alpha="255">
        <red>132</red>
        <green>239</green>
        <blue>240</blue>
      </color>
      </brush>
    </colorrole>
    </disabled>
  </palette>
  </property>
  <property name="windowTitle">
  <string>平均绩点计算器</string>
  </property>
  <property name="windowIcon">
  <iconset resource="icon.qrc">
    <normaloff>:/cal.ico</normaloff>:/cal.ico</iconset>
  </property>
  <widget class="QWidget" name="centralWidget">
  <layout class="QGridLayout" name="gridLayout">
    <item row="1" column="3">
    <widget class="QLabel" name="label_2">
      <property name="text">
      <string>绩点</string>
      </property>
    </widget>
    </item>
    <item row="0" column="0">
    <widget class="QLabel" name="label_3">
      <property name="text">
      <string>平均绩点:</string>
      </property>
    </widget>
    </item>
    <item row="0" column="2">
    <widget class="QLabel" name="pjjd">
      <property name="text">
      <string>等待计算</string>
      </property>
    </widget>
    </item>
    <item row="0" column="4">
    <widget class="QPushButton" name="add">
      <property name="text">
      <string>增加</string>
      </property>
    </widget>
    </item>
    <item row="3" column="4">
    <widget class="QPushButton" name="cal">
      <property name="text">
      <string>计算</string>
      </property>
    </widget>
    </item>
    <item row="1" column="1">
    <widget class="QLabel" name="label">
      <property name="text">
      <string>学分</string>
      </property>
    </widget>
    </item>
    <item row="2" column="0" colspan="4">
    <widget class="QScrollArea" name="scrollArea">
      <property name="widgetResizable">
      <bool>true</bool>
      </property>
      <widget class="QWidget" name="scrollAreaWidgetContents">
      <property name="geometry">
        <rect>
        <x>0</x>
        <y>0</y>
        <width>398</width>
        <height>252</height>
        </rect>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
        <layout class="QVBoxLayout" name="VLX">
          <item>
          <widget class="QLineEdit" name="LEX1">
            <property name="text">
            <string/>
            </property>
          </widget>
          </item>
          <item>
          <widget class="QLineEdit" name="LEX2"/>
          </item>
        </layout>
        </item>
        <item>
        <layout class="QVBoxLayout" name="VLJ">
          <item>
          <widget class="QLineEdit" name="LEJ1"/>
          </item>
          <item>
          <widget class="QLineEdit" name="LEJ2"/>
          </item>
        </layout>
        </item>
      </layout>
      <zorder></zorder>
      <zorder></zorder>
      </widget>
    </widget>
    </item>
  </layout>
  <zorder>label_2</zorder>
  <zorder>label_3</zorder>
  <zorder>pjjd</zorder>
  <zorder>add</zorder>
  <zorder>cal</zorder>
  <zorder>label</zorder>
  <zorder>scrollArea</zorder>
  </widget>
  <widget class="QMenuBar" name="menuBar">
  <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>520</width>
    <height>23</height>
    </rect>
  </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
  <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
  </attribute>
  <attribute name="toolBarBreak">
    <bool>false</bool>
  </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="icon.qrc"/>
 </resources>
 <connections/>
</ui>

main,cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
   
    return a.exec();
}

linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/4/26 15:20:42
这个真拿不出手~~~