目录
程序简介
一个功能非常简单的程序,用来求两个数的和。本程序侧重于对 GUI 程序的设计进行一个简单的练习。程序主界面如下:
用法不言自明。然后还有一些简单的菜单命令:
\text{\ \ \ \ \ \ \ }
\text{\ \ \ \ \ \ \ }
以及“关于”页面:
- 在两个输入框分别输入两个加数,点击
计算
按钮,可以显示计算结果 - 点击
重置
按钮,可以清空输入框和结果的内容 - 点击
文件->退出
实现程序的关闭 操作
菜单也可实现计算
和重置
- 点击
帮助->关于
弹出“关于”窗口,“关于”窗口提供关闭
按钮
设计思路
图形界面使用 Qt Designer 设计即可,不再赘述,两个窗口,一个主窗口一个“关于”窗口。
在主界面源文件Add_GUI.cpp
中,定义clickCalc()
函数,其功能是读取两个输入框中的数据,执行计算并显示结果,详细设计如下:
void Add_GUI::clickCalc()
{
QString str = ui.addnum1->text(); //定义字符串类,获取第一个输入框的数据
double add1 = str.toDouble(); //把第一个输入的数据转换成浮点数
str = ui.addnum2->text(); //获取第二个输入框的数据
double add2 = str.toDouble(); //把第二个输入的数据转换成浮点数
double sum = add(add1, add2); //执行计算
str = str.sprintf("%lf", sum); //把计算结果转换为字符串
ui.result->setText(str); //更新结果框内容
}
此外还要在主窗口类的构造函数中定义信号和槽,通过点击计算
按钮来调用该函数:
connect(ui.calc, SIGNAL(clicked()), this, SLOT(clickCalc()));
和功能 1 类似,只需要设计一个函数clickReset()
,用来清空输入框和结果框的显示数据:
}
void Add_GUI::clickReset()
{
ui.addnum1->setText("");
ui.addnum2->setText("");
ui.result->setText("");
}
定义相应的信号和槽,通过点击重置
按钮来调用该函数:
connect(ui.reset, SIGNAL(clicked()), this, SLOT(clickReset()));
直接在 Qt Designer 中定义信号和槽即可:
还是在Qt Designer 中定义信号和槽:
实现弹出窗口,在主界面源文件Add_GUI.cpp
中,定义open_about()
函数,实现打开新窗口:
void Add_GUI::open_about()
{
son = new About;
son->show();
}
同理,定义信号和槽:
connect(ui.about, SIGNAL(triggered()), this, SLOT(open_about()));
实现
Add_GUI.ui
、About.ui
、ui_Add_GUI.h
和ui_About.h
是利用 Qt Designer 进行 GUI 设计时自动生成的,一般不需要对其做出修改。
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Add_GUIClass</class>
<widget class="QMainWindow" name="Add_GUIClass">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>230</width>
<height>300</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>230</width>
<height>300</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>230</width>
<height>300</height>
</size>
</property>
<property name="windowTitle">
<string>求和程序</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="title">
<property name="geometry">
<rect>
<x>20</x>
<y>12</y>
<width>71</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
<stylestrategy>PreferAntialias</stylestrategy>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: rgb(0, 120, 255);</string>
</property>
<property name="text">
<string>求和程序</string>
</property>
</widget>
<widget class="QLabel" name="addtext1">
<property name="geometry">
<rect>
<x>20</x>
<y>59</y>
<width>54</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>加数 1</string>
</property>
</widget>
<widget class="QLabel" name="addtext2">
<property name="geometry">
<rect>
<x>20</x>
<y>91</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>加数 2</string>
</property>
</widget>
<widget class="QLineEdit" name="addnum1">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
</font>
</property>
<property name="placeholderText">
<string/>
</property>
</widget>
<widget class="QLineEdit" name="addnum2">
<property name="geometry">
<rect>
<x>80</x>
<y>90</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
</font>
</property>
</widget>
<widget class="QLabel" name="addtext2_2">
<property name="geometry">
<rect>
<x>20</x>
<y>180</y>
<width>54</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>计算结果</string>
</property>
</widget>
<widget class="QPushButton" name="calc">
<property name="geometry">
<rect>
<x>80</x>
<y>130</y>
<width>75</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
</font>
</property>
<property name="text">
<string>计算</string>
</property>
</widget>
<widget class="QPushButton" name="reset">
<property name="geometry">
<rect>
<x>80</x>
<y>220</y>
<width>75</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
</font>
</property>
<property name="text">
<string>重置</string>
</property>
</widget>
<widget class="QLineEdit" name="result">
<property name="geometry">
<rect>
<x>80</x>
<y>180</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>230</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="file">
<property name="title">
<string>文件</string>
</property>
<addaction name="exit"/>
</widget>
<widget class="QMenu" name="help">
<property name="title">
<string>帮助</string>
</property>
<addaction name="about"/>
</widget>
<widget class="QMenu" name="menu">
<property name="title">
<string>操作</string>
</property>
<addaction name="menu_calc"/>
<addaction name="menu_reset"/>
</widget>
<addaction name="file"/>
<addaction name="menu"/>
<addaction name="help"/>
</widget>
<action name="exit">
<property name="text">
<string>退出</string>
</property>
</action>
<action name="about">
<property name="text">
<string>关于</string>
</property>
</action>
<action name="menu_calc">
<property name="text">
<string>计算</string>
</property>
</action>
<action name="menu_reset">
<property name="text">
<string>重置</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="Add_GUI.qrc"/>
</resources>
<connections>
<connection>
<sender>exit</sender>
<signal>triggered()</signal>
<receiver>Add_GUIClass</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>119</x>
<y>227</y>
</hint>
</hints>
</connection>
<connection>
<sender>menu_calc</sender>
<signal>triggered()</signal>
<receiver>calc</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>117</x>
<y>167</y>
</hint>
</hints>
</connection>
<connection>
<sender>menu_reset</sender>
<signal>triggered()</signal>
<receiver>reset</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>117</x>
<y>237</y>
</hint>
</hints>
</connection>
</connections>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>About</class>
<widget class="QWidget" name="About">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>330</width>
<height>160</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>330</width>
<height>160</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>330</width>
<height>160</height>
</size>
</property>
<property name="windowTitle">
<string>关于</string>
</property>
<widget class="QLabel" name="ver">
<property name="geometry">
<rect>
<x>20</x>
<y>90</y>
<width>161</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>版本:1.5</string>
</property>
</widget>
<widget class="QLabel" name="name">
<property name="geometry">
<rect>
<x>20</x>
<y>58</y>
<width>361</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>作者:张沐雨(zhangmuyu2000@outlook.com)</string>
</property>
</widget>
<widget class="QLabel" name="title">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>54</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>求和程序</string>
</property>
</widget>
<widget class="QPushButton" name="yes">
<property name="geometry">
<rect>
<x>130</x>
<y>115</y>
<width>75</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
</font>
</property>
<property name="text">
<string>确定</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>yes</sender>
<signal>clicked()</signal>
<receiver>About</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>197</x>
<y>151</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>102</y>
</hint>
</hints>
</connection>
</connections>
</ui>
/********************************************************************************
** Form generated from reading UI file 'Add_GUI.ui'
**
** Created by: Qt User Interface Compiler version 5.14.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_ADD_GUI_H
#define UI_ADD_GUI_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Add_GUIClass
{
public:
QAction *exit;
QAction *about;
QAction *menu_calc;
QAction *menu_reset;
QWidget *centralWidget;
QLabel *title;
QLabel *addtext1;
QLabel *addtext2;
QLineEdit *addnum1;
QLineEdit *addnum2;
QLabel *addtext2_2;
QPushButton *calc;
QPushButton *reset;
QLineEdit *result;
QMenuBar *menuBar;
QMenu *file;
QMenu *help;
QMenu *menu;
void setupUi(QMainWindow *Add_GUIClass)
{
if (Add_GUIClass->objectName().isEmpty())
Add_GUIClass->setObjectName(QString::fromUtf8("Add_GUIClass"));
Add_GUIClass->setEnabled(true);
Add_GUIClass->resize(230, 300);
Add_GUIClass->setMinimumSize(QSize(230, 300));
Add_GUIClass->setMaximumSize(QSize(230, 300));
exit = new QAction(Add_GUIClass);
exit->setObjectName(QString::fromUtf8("exit"));
about = new QAction(Add_GUIClass);
about->setObjectName(QString::fromUtf8("about"));
menu_calc = new QAction(Add_GUIClass);
menu_calc->setObjectName(QString::fromUtf8("menu_calc"));
menu_reset = new QAction(Add_GUIClass);
menu_reset->setObjectName(QString::fromUtf8("menu_reset"));
centralWidget = new QWidget(Add_GUIClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
title = new QLabel(centralWidget);
title->setObjectName(QString::fromUtf8("title"));
title->setGeometry(QRect(20, 12, 71, 31));
QFont font;
font.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
font.setPointSize(12);
font.setBold(true);
font.setWeight(75);
font.setStyleStrategy(QFont::PreferAntialias);
title->setFont(font);
title->setStyleSheet(QString::fromUtf8("color: rgb(0, 120, 255);"));
addtext1 = new QLabel(centralWidget);
addtext1->setObjectName(QString::fromUtf8("addtext1"));
addtext1->setGeometry(QRect(20, 59, 54, 20));
QFont font1;
font1.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
font1.setPointSize(10);
addtext1->setFont(font1);
addtext2 = new QLabel(centralWidget);
addtext2->setObjectName(QString::fromUtf8("addtext2"));
addtext2->setGeometry(QRect(20, 91, 54, 16));
addtext2->setFont(font1);
addnum1 = new QLineEdit(centralWidget);
addnum1->setObjectName(QString::fromUtf8("addnum1"));
addnum1->setEnabled(true);
addnum1->setGeometry(QRect(80, 60, 113, 20));
QFont font2;
font2.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
addnum1->setFont(font2);
addnum2 = new QLineEdit(centralWidget);
addnum2->setObjectName(QString::fromUtf8("addnum2"));
addnum2->setGeometry(QRect(80, 90, 113, 20));
addnum2->setFont(font2);
addtext2_2 = new QLabel(centralWidget);
addtext2_2->setObjectName(QString::fromUtf8("addtext2_2"));
addtext2_2->setGeometry(QRect(20, 180, 54, 21));
addtext2_2->setFont(font1);
calc = new QPushButton(centralWidget);
calc->setObjectName(QString::fromUtf8("calc"));
calc->setGeometry(QRect(80, 130, 75, 31));
calc->setFont(font2);
reset = new QPushButton(centralWidget);
reset->setObjectName(QString::fromUtf8("reset"));
reset->setGeometry(QRect(80, 220, 75, 31));
reset->setFont(font2);
result = new QLineEdit(centralWidget);
result->setObjectName(QString::fromUtf8("result"));
result->setGeometry(QRect(80, 180, 113, 20));
QFont font3;
font3.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
font3.setBold(true);
font3.setWeight(75);
result->setFont(font3);
result->setReadOnly(true);
Add_GUIClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(Add_GUIClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 230, 22));
file = new QMenu(menuBar);
file->setObjectName(QString::fromUtf8("file"));
help = new QMenu(menuBar);
help->setObjectName(QString::fromUtf8("help"));
menu = new QMenu(menuBar);
menu->setObjectName(QString::fromUtf8("menu"));
Add_GUIClass->setMenuBar(menuBar);
menuBar->addAction(file->menuAction());
menuBar->addAction(menu->menuAction());
menuBar->addAction(help->menuAction());
file->addAction(exit);
help->addAction(about);
menu->addAction(menu_calc);
menu->addAction(menu_reset);
retranslateUi(Add_GUIClass);
QObject::connect(exit, SIGNAL(triggered()), Add_GUIClass, SLOT(close()));
QObject::connect(menu_calc, SIGNAL(triggered()), calc, SLOT(click()));
QObject::connect(menu_reset, SIGNAL(triggered()), reset, SLOT(click()));
QMetaObject::connectSlotsByName(Add_GUIClass);
} // setupUi
void retranslateUi(QMainWindow *Add_GUIClass)
{
Add_GUIClass->setWindowTitle(QCoreApplication::translate("Add_GUIClass", "\346\261\202\345\222\214\347\250\213\345\272\217", nullptr));
exit->setText(QCoreApplication::translate("Add_GUIClass", "\351\200\200\345\207\272", nullptr));
about->setText(QCoreApplication::translate("Add_GUIClass", "\345\205\263\344\272\216", nullptr));
menu_calc->setText(QCoreApplication::translate("Add_GUIClass", "\350\256\241\347\256\227", nullptr));
menu_reset->setText(QCoreApplication::translate("Add_GUIClass", "\351\207\215\347\275\256", nullptr));
title->setText(QCoreApplication::translate("Add_GUIClass", "\346\261\202\345\222\214\347\250\213\345\272\217", nullptr));
addtext1->setText(QCoreApplication::translate("Add_GUIClass", "\345\212\240\346\225\260 1", nullptr));
addtext2->setText(QCoreApplication::translate("Add_GUIClass", "\345\212\240\346\225\260 2", nullptr));
addnum1->setPlaceholderText(QString());
addtext2_2->setText(QCoreApplication::translate("Add_GUIClass", "\350\256\241\347\256\227\347\273\223\346\236\234", nullptr));
calc->setText(QCoreApplication::translate("Add_GUIClass", "\350\256\241\347\256\227", nullptr));
reset->setText(QCoreApplication::translate("Add_GUIClass", "\351\207\215\347\275\256", nullptr));
file->setTitle(QCoreApplication::translate("Add_GUIClass", "\346\226\207\344\273\266", nullptr));
help->setTitle(QCoreApplication::translate("Add_GUIClass", "\345\270\256\345\212\251", nullptr));
menu->setTitle(QCoreApplication::translate("Add_GUIClass", "\346\223\215\344\275\234", nullptr));
} // retranslateUi
};
namespace Ui {
class Add_GUIClass: public Ui_Add_GUIClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_ADD_GUI_H
/********************************************************************************
** Form generated from reading UI file 'About.ui'
**
** Created by: Qt User Interface Compiler version 5.14.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_ABOUT_H
#define UI_ABOUT_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_About
{
public:
QLabel *ver;
QLabel *name;
QLabel *title;
QPushButton *yes;
void setupUi(QWidget *About)
{
if (About->objectName().isEmpty())
About->setObjectName(QString::fromUtf8("About"));
About->resize(330, 160);
About->setMinimumSize(QSize(330, 160));
About->setMaximumSize(QSize(330, 160));
ver = new QLabel(About);
ver->setObjectName(QString::fromUtf8("ver"));
ver->setGeometry(QRect(20, 90, 161, 16));
QFont font;
font.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
font.setPointSize(10);
ver->setFont(font);
name = new QLabel(About);
name->setObjectName(QString::fromUtf8("name"));
name->setGeometry(QRect(20, 58, 361, 20));
name->setFont(font);
title = new QLabel(About);
title->setObjectName(QString::fromUtf8("title"));
title->setGeometry(QRect(20, 20, 54, 21));
QFont font1;
font1.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
font1.setPointSize(10);
font1.setBold(true);
font1.setWeight(75);
title->setFont(font1);
yes = new QPushButton(About);
yes->setObjectName(QString::fromUtf8("yes"));
yes->setGeometry(QRect(130, 115, 75, 31));
QFont font2;
font2.setFamily(QString::fromUtf8("Microsoft YaHei UI"));
yes->setFont(font2);
retranslateUi(About);
QObject::connect(yes, SIGNAL(clicked()), About, SLOT(close()));
QMetaObject::connectSlotsByName(About);
} // setupUi
void retranslateUi(QWidget *About)
{
About->setWindowTitle(QCoreApplication::translate("About", "\345\205\263\344\272\216", nullptr));
ver->setText(QCoreApplication::translate("About", "\347\211\210\346\234\254\357\274\2321.5", nullptr));
name->setText(QCoreApplication::translate("About", "\344\275\234\350\200\205\357\274\232\345\274\240\346\262\220\351\233\250\357\274\210zhangmuyu2000@outlook.com\357\274\211", nullptr));
title->setText(QCoreApplication::translate("About", "\346\261\202\345\222\214\347\250\213\345\272\217", nullptr));
yes->setText(QCoreApplication::translate("About", "\347\241\256\345\256\232", nullptr));
} // retranslateUi
};
namespace Ui {
class About: public Ui_About {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_ABOUT_H
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_Add_GUI.h"
#include "About.h"
class Add_GUI : public QMainWindow
{
Q_OBJECT
public:
Add_GUI(QWidget *parent = Q_NULLPTR);
private:
Ui::Add_GUIClass ui;
About* son;
int a = 0, b = 0, sum = 0;
private slots:
void open_about();
void clickCalc();
void clickReset();
};
#pragma once
#include <QWidget>
#include "ui_About.h"
class About : public QWidget
{
Q_OBJECT
public:
About(QWidget *parent = Q_NULLPTR);
~About();
private:
Ui::About ui;
};
#include "Add_GUI.h"
double add(double, double);
Add_GUI::Add_GUI(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.about, SIGNAL(triggered()), this, SLOT(open_about()));
connect(ui.calc, SIGNAL(clicked()), this, SLOT(clickCalc()));
connect(ui.reset, SIGNAL(clicked()), this, SLOT(clickReset()));
}
void Add_GUI::open_about()
{
son = new About;
son->show();
}
void Add_GUI::clickCalc()
{
QString str = ui.addnum1->text();
double add1 = str.toDouble();
str = ui.addnum2->text();
double add2 = str.toDouble();
double sum = add(add1, add2);
str = str.sprintf("%lf", sum);
ui.result->setText(str);
}
void Add_GUI::clickReset()
{
ui.addnum1->setText("");
ui.addnum2->setText("");
ui.result->setText("");
}
#include "About.h"
#include "Add_GUI.h"
About::About(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
About::~About()
{
}
double add(double a, double b)
{
return a + b;
}
#include "Add_GUI.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Add_GUI w;
w.show();
return a.exec();
}