1
项目创建:
2
运行效果图
QT版本计算器
mainwindow.h
main.cpp
mainwindow.cpp
mainwindow.ui
文件1 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
第二个文件 main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
第三个文件 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//
void MainWindow::on_pushButton_clicked()
{
// 1输入什么就显示什么
// QString str = ui->lineEdit->text();
// ui->label->setText(str);
// 2 C版本的输入
// char buf[100] = {0};
// strcpy(buf,"Hello World!");
// QString str = buf;
// ui->label->setText(str);
// 3 把 str的字符挖出来
// char buf[100] = {0};
// QString str = ui->lineEdit->text();
// const char *p = str.toStdString().data();
// strcpy(buf,p);
// str += " World";
// ui->label->setText(str);
// 4十进制计算器
// QString str1 = ui ->lineEdit->text();
// QString str2 = ui ->lineEdit_2->text();
// int a = str1.toInt();
// int b = str2.toInt();
// int c = a+b;
// QString str3 = QString::number(c);
// ui->label->setText(str3);
// 5 十六进制计算器
// QString str1 = ui ->lineEdit->text();
// QString str2 = ui ->lineEdit_2->text();
// int a = str1.toInt(0,16);
// int b = str2.toInt(0,16);
// int c = a+b;
// QString str3 = QString::number(c,16);
// ui->label->setText(str3);
// 6 8八进制计算器
QString str1 = ui ->lineEdit->text();
QString str2 = ui ->lineEdit_2->text();
int a = str1.toInt(0,8);
int b = str2.toInt(0,8);
int c = a+b;
QString str3 = QString::number(c,8);
ui->label->setText(str3);
}
第四个文件 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>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>280</x>
<y>70</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>30</x>
<y>70</y>
<width>221</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>130</y>
<width>221</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>221</width>
<height>31</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuChunli">
<property name="title">
<string>chunli</string>
</property>
</widget>
<addaction name="menuChunli"/>
</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>