代码实现界面和槽
代码实现界面和槽 在上述工程的dialog.h中添加如下加黑代码: 加入头文件: #include <QLabel> #include <QLineEdit> #include <QPushButton> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: QLabel *label1; QLineEdit *lineEdit1,*lineEdit2; QPushButton *button1; }
代码实现界面和槽 在dialog.cpp 中添加如下代码: #include <QGridLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent) { label1=new QLabel(this); label1->setText(tr("")); lineEdit1=new QLineEdit(this); lineEdit2=new QLineEdit(this); button1=new QPushButton(this); button1->setText(tr("求和")); QGridLayout *mainLayout=new QGridLayout(this); mainLayout->addWidget(lineEdit1,0,0); mainLayout->addWidget(lineEdit2,0,1); mainLayout->addWidget(button1,1,0); mainLayout->addWidget(label1,1,1); }
代码实现界面和槽 打开dialog.h文件,在类构造函数和控件成员声明后,添加如下代码: class Dialog : public QDialog { ... ... QPushButton *button; private slots: void Add(); };
代码实现界面和槽 打开dialog.cpp 文件, 在构造函数中添加如下代码: Dialog::Dialog(QWidget *parent) : QDialog(parent) { ... ... connect(button1,SIGNAL(clicked()),this,SLOT(Add())); }
代码实现界面和槽 在Add()中实现求和功能,代码如下: void Dialog::Add() { QString s1 = lineEdit1->text(); QString s2 = lineEdit2->text(); int i1 = s1.toInt(); int i2 = s2.toInt(); label1->setText(QString::number(i1 + i2)); }