注:本文部分或全部内容摘自《Qt5开发及实例 陆文周 主编》
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include <QWidget> #include <QSerialPort> #include <QSerialPortInfo> class Dialog : public QDialog { //Q_OBJECT 宏的作用是启动Qt元对象系统的一些特性(如:支持信号和槽),必须放到类定义的私有区; Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); private: // 控件变量指针 QLabel *label1, *label2; QLineEdit *lineEdit; QPushButton *button; QSerialPort *serial; private slots: //信号槽方法接口 void showArea(); }; #endif // DIALOG_Hdialog.cpp
#include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QGridLayout> #include <QList> #include <QMap> #include <QHash> #include <QLinkedList> #include <QVector> #include "dialog.h" const static double PI = 3.1415926; // Dialog类构造方法 Dialog::Dialog(QWidget *parent):QDialog(parent) { // 创建控件实例 label1 = new QLabel(this); label1->setText(tr("请输入半径:")); lineEdit = new QLineEdit(this); label2 = new QLabel(this); label2->setText(tr("面积")); button = new QPushButton(this); button->setText(tr("计算")); // 添加控制到指定布局 QGridLayout *gridLayout = new QGridLayout(this); gridLayout->addWidget(label1, 0, 0); gridLayout->addWidget(lineEdit, 0, 1); gridLayout->addWidget(label2, 1, 0); gridLayout->addWidget(button, 1, 1); // 连接信号槽 connect(button, SIGNAL(clicked()), this, SLOT(showArea())); connect(lineEdit, SIGNAL(textChanged()), this, SLOT(showArea())); }; // Dialog析构方法 Dialog::~Dialog() { delete(label1); delete(label2); delete(lineEdit); delete(button); }; // 信号槽功能实现 void Dialog::showArea() { bool ok; QString tempStr; QString valueStr = lineEdit->text(); int valueInt = valueStr.toInt(&ok); double area = valueInt * valueInt * PI; label2->setText(tempStr.setNum(area)); qDebug("tempStr:%s", tempStr.toStdString().c_str()); };mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_radiusLineEdit_textChanged(const QString &arg1); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" const static double PI = 3.1415926; //const static double E = 2.71828; //const static double S2 = 1.414; //const static double FI = 0.618; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } // UI添加信号槽 void MainWindow::on_pushButton_clicked() { bool ok; qDebug("on pushbutton clicked"); QString tempStr; QString valueStr = ui->radiusLineEdit->text(); int valueInt = valueStr.toInt(&ok); double area = valueInt * valueInt *PI; // ui->valueLineEdit->setText(tempStr.setNum(area)); } // UI添加信号槽 void MainWindow::on_radiusLineEdit_textChanged(const QString &arg1) { bool ok; qDebug("on lineedit textchanged"); QString tempStr; QString valueStr = ui->radiusLineEdit->text(); int valueInt = valueStr.toInt(&ok); double area = valueInt * valueInt * PI; ui->valueLineEdit->setText(tempStr.setNum(area)); }main.cpp
#include "mainwindow.h" #include "dialog.h" #include <QApplication> #include <QLabel> #include <QButtonGroup> #include <QPushButton> #include <QWidget> #include <QVBoxLayout> #include <QFile> int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow win; win.show(); Dialog dialog; dialog.show(); // 显示Dialog对话框 #if 0//代码实现控件,因为控制没有指定父窗口window, 所以创建控件都是在独立窗口显示 QLabel label("hello word"); label.setText("Hello Word new text!"); label.show(); QLabel *label2 = new QLabel("Label 2"); label2->setAttribute(Qt::WA_DeleteOnClose); label2->show(); QPushButton button("Quit"); QObject::connect(&button, &QPushButton::clicked, &QApplication::quit); button.show(); #endif return app.exec(); } 运行效果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201021181854250.gif#pic_center) 添加链接描述