现在的位置: 首页 > 综合 > 正文

用Qt写的平均绩点计算器

2013年05月21日 ⁄ 综合 ⁄ 共 5178字 ⁄ 字号 评论关闭

之前用C#的WPF写了一个平均绩点计算器,发到学校的贴吧里,反响还不错。不过没过几个月,就有人写了一个网络版的计算器,把我盖过去了。“既生瑜,何生亮?”开个玩笑。

转到Qt之后,学了一阵子,想练练手,就用Qt写了一个平均绩点计算器。

源码如下

mainwindow,h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QLineEdit>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    double result;
    QList<QLineEdit*> LX,LJ;
    int count;

public slots:
    void slot_add() ;
    void slot_cal();
};

#endif // MAINWINDOW_H

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();
}

抱歉!评论已关闭.