Value passing between Qt forms (Code backup), qt forms

Source: Internet
Author: User

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();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.