自学Qt第二天,自己写的一个简单的计算器,最后运行效果如下
利用Qt编写图形界面,一种是利用Qt-design,另外一种是代码编写
1.利用Qt-design编写,
Qt-design可以提高效率,其界面如下
代码如下:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>//是一个主窗口,它提供了构建用户应用程序界面的框架,
//它由ToolBars、Dock Widget、Central Widget、Status Bar组成
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void sum();
void mistake();
void ji();
void shang();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(sum()));
QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(mistake()));
QObject::connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(ji()));
QObject::connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(shang()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::sum()
{
int first=ui->lineEdit->text().toInt();
int second=ui->lineEdit_2->text().toInt();
int result=first+second;
ui->lineEdit_3->setText(QString::number(result));
}
void MainWindow::mistake()
{
int first=ui->lineEdit_4->text().toInt();
int second=ui->lineEdit_5->text().toInt();
int result=first-second;
ui->lineEdit_6->setText(QString::number(result));
}
void MainWindow::ji()
{
int first=ui->lineEdit_7->text().toInt();
int second=ui->lineEdit_8->text().toInt();
int result=first*second;
ui->lineEdit_9->setText(QString::number(result));
}
void MainWindow::shang()
{
int first=ui->lineEdit_10->text().toInt();
int second=ui->lineEdit_11->text().toInt();
int result=first/second;
ui->lineEdit_12->setText(QString::number(result));
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])//函数的入口,argc表示命令行数量,argv表示命令行数组
{
QApplication a(argc, argv);//必须的,任何qt的窗口系统被使用前都要创建QApplication对象
MainWindow w;//创建一个主窗口对象
w.show();//调用show显示主窗口
return a.exec();//
}
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>567</width>
<height>283</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>0</x>
<y>60</y>
<width>518</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>+</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_3"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>计算</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>0</x>
<y>100</y>
<width>518</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="lineEdit_4"/>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_5"/>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_6"/>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>计算</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>0</x>
<y>150</y>
<width>518</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLineEdit" name="lineEdit_7"/>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>*</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_8"/>
</item>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_9"/>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>计算</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>0</x>
<y>190</y>
<width>518</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="lineEdit_10"/>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>/</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_11"/>
</item>
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_12"/>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>计算</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>567</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/>
<connections/>
</ui>