- 按钮点击获取文本框输入
1 void Dialog::on_pushButton_clicked() 2 { 3 //获取文本输入 4 QString vstr = ui->lineEdit->text(); 5 6 //判断是否转换成功 7 bool isok; 8 //转换 9 int value = vstr.toInt(&isok); 10 double area = value*value*PI; 11 QString tempstr; 12 ui->lineEdit_2->setText(tempstr.setNum(area)); 13 }
- 文本框内容改变执行的操作
void Dialog::on_lineEdit_textChanged(const QString &arg1) { this->on_pushButton_clicked(); }
手动实现QT的界面
- dialog.h添加按钮,文本框,标签等头文件
1 #include <QDialog> 2 #include <QPushButton> 3 #include <QLineEdit> 4 #include <QLabel>
- 在dialog.h类中添加按钮,文本框,标签的指针,并添加按钮的触发函数
1 private: 2 Ui::Dialog *ui; 3 //图形控件 4 QLabel *label1,*label2; 5 QLineEdit *edit1,*edit2; 6 QPushButton *button; 7 8 //触发机制 9 private slots: 10 void showit();
- 在dialog.cpp中初始化,并创建布局
- 创建布局头文件
1 #include "QGridLayout"//布局
- 初始化
1 //加上this显示在当前窗口 2 button = new QPushButton(this); 3 button->setText(tr("计算")); 4 //button->show(); 5 button->resize(100,100); 6 7 edit1 = new QLineEdit(this); 8 9 edit2 = new QLineEdit(this); 10 11 label1 = new QLabel(this); 12 13 label1->setText("输入半径:"); 14 15 label2 = new QLabel(this); 16 17 label2->setText("输出面积:");
- 创建布局
1 //网格布局 2 QGridLayout *mylayout = new QGridLayout(this); 3 mylayout->addWidget(label1,0,0); 4 mylayout->addWidget(label2,1,0); 5 mylayout->addWidget(edit1,0,1); 6 mylayout->addWidget(edit2,1,1); 7 mylayout->addWidget(button,2,0);
- 关联按钮的函数或者文本框变化的函数
1 //clicked触发 关联 2 connect(button,SIGNAL(clicked(bool)),this,SLOT(showit())); 3 connect(edit1,SIGNAL(textChanged(QString)),this,SLOT(showit()));
- 按钮点击函数实现
1 void Dialog::showit() 2 { 3 //获取文本输入 4 QString vstr = edit1->text(); 5 6 //判断是否转换成功 7 bool isok; 8 //转换 9 int value = vstr.toInt(&isok); 10 double area = value*value*PI; 11 QString tempstr; 12 edit2->setText(tempstr.setNum(area)); 13 }
完整代码:
- dialog.h
1 #ifndef DIALOG_H 2 #define DIALOG_H 3 4 #include <QDialog> 5 #include <QPushButton> 6 #include <QLineEdit> 7 #include <QLabel> 8 9 namespace Ui { 10 class Dialog; 11 } 12 13 class Dialog : public QDialog 14 { 15 Q_OBJECT 16 17 public: 18 explicit Dialog(QWidget *parent = 0); 19 ~Dialog(); 20 21 private: 22 Ui::Dialog *ui; 23 //图形控件 24 QLabel *label1,*label2; 25 QLineEdit *edit1,*edit2; 26 QPushButton *button; 27 28 //触发机制 29 private slots: 30 void showit(); 31 }; 32 33 #endif // DIALOG_H
- dialog.cpp
1 #include "dialog.h" 2 #include "ui_dialog.h" 3 #include "QGridLayout"//布局 4 #define PI 3.14159 5 6 Dialog::Dialog(QWidget *parent) : 7 QDialog(parent), 8 ui(new Ui::Dialog) 9 { 10 //加上this显示在当前窗口 11 button = new QPushButton(this); 12 button->setText(tr("计算")); 13 //button->show(); 14 button->resize(100,100); 15 16 edit1 = new QLineEdit(this); 17 18 edit2 = new QLineEdit(this); 19 20 label1 = new QLabel(this); 21 22 label1->setText("输入半径:"); 23 24 label2 = new QLabel(this); 25 26 label2->setText("输出面积:"); 27 28 //网格布局 29 QGridLayout *mylayout = new QGridLayout(this); 30 mylayout->addWidget(label1,0,0); 31 mylayout->addWidget(label2,1,0); 32 mylayout->addWidget(edit1,0,1); 33 mylayout->addWidget(edit2,1,1); 34 mylayout->addWidget(button,2,0); 35 36 //clicked触发 关联 37 connect(button,SIGNAL(clicked(bool)),this,SLOT(showit())); 38 connect(edit1,SIGNAL(textChanged(QString)),this,SLOT(showit())); 39 ui->setupUi(this); 40 } 41 42 Dialog::~Dialog() 43 { 44 delete ui; 45 } 46 47 void Dialog::showit() 48 { 49 //获取文本输入 50 QString vstr = edit1->text(); 51 52 //判断是否转换成功 53 bool isok; 54 //转换 55 int value = vstr.toInt(&isok); 56 double area = value*value*PI; 57 QString tempstr; 58 edit2->setText(tempstr.setNum(area)); 59 }
- main.cpp
1 #include "dialog.h" 2 #include <QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 Dialog w; 8 w.show(); 9 10 return a.exec(); 11 }