Value passing between Qt forms (Code backup), qt forms
At the beginning, I was confused by the cloud. Now I know a little bit about it. Now assume there is a form, a MainWindow ,:
Click PushButton to transfer the content in the text box to MainWindow and display it as a Label. I have already painted the interface in advance. The following is the backup code:
Form. h:
#ifndef FORM_H#define FORM_H#include <QWidget>namespace Ui {class Form;}class Form : public QWidget{ Q_OBJECTpublic: explicit Form(QWidget *parent = 0); ~Form();signals: void sendData(QString);private slots: void on_sendBtn_clicked();private: Ui::Form *ui;};#endif // FORM_H
Form. cpp:
#include "form.h"#include "ui_form.h"Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form){ ui->setupUi(this);}Form::~Form(){ delete ui;}void Form::on_sendBtn_clicked(){ emit sendData(ui->lineE->text());}
MainWindow. h:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include "form.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();private slots: void receiveData(QString data);private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
MainWindow. cpp:
# Include "mainwindow. h "# include" ui_main1_1_h "MainWindow: MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui: MainWindow) {ui-> setupUi (this ); // pass the value test Form * form = new Form; form-> setGeometry (100,300,400,400); form-> show (); connect (form, SIGNAL (sendData (QString )), this, SLOT (receiveData (QString);} MainWindow ::~ MainWindow () {delete ui;} void MainWindow: receiveData (QString data) {ui-> label-> setText (data );}
Main. cpp:
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}