Transfer Data Between Two Forms
-
This is my code:
send.h :
#ifndef SEND_H #define SEND_H #include <QMainWindow> namespace Ui { class send; } class send : public QMainWindow { Q_OBJECT public: explicit send(QWidget *parent = 0); ~send(); private slots: void on_pushButton_clicked(); void showReceiveForm(); signals: void newTextEntered(const QString &text); private: Ui::send *ui; }; #endif // SEND_H
send.cpp :
#include "send.h" #include "ui_send.h" #include "chaild.h" #include "ui_chaild.h" send::send(QWidget *parent) : QMainWindow(parent), ui(new Ui::send) { ui->setupUi(this); } send::~send() { delete ui; } void send::on_pushButton_clicked() { emit newTextEntered(ui->lineEdit->text()); chaild ss; ss.show(); ss.exec(); }
chaild.h :
#ifndef CHAILD_H #define CHAILD_H #include <QDialog> namespace Ui { class chaild; } class chaild : public QDialog { Q_OBJECT public: QString ss; explicit chaild(QWidget *parent = 0); ~chaild(); private slots: void on_spinBox_valueChanged(int arg1); public slots: void onNewTextEntered(const QString &text); private: Ui::chaild *ui; };
chaild.cpp :
#include "chaild.h" #include "ui_chaild.h" #include "send.h" #include "ui_send.h" chaild::chaild(QWidget *parent) : QDialog(parent), ui(new Ui::chaild) { ui->setupUi(this); send st; connect(&st,SIGNAL(newTextEntered(QString)),this,SLOT(onNewTextEntered(QString))); } chaild::~chaild() { delete ui; } void chaild::on_spinBox_valueChanged(int age) { ui->spinBox->setValue(age); } void chaild::onNewTextEntered(const QString &text) { // Adding a new item to the list widget ui->textEdit->setText(text); }
please tell me how to pass the data from send form to chaild form
[edit: koahnig, code tags introduced]
-
Hi and welcome to devnet forums
I would suggest to you to have a look to the different examples for instance this http://doc.qt.io/qt-5/qtwidgets-dialogs-standarddialogs-example.html
The problem you are generating is that these dialogs cross-dependencies.
send is derived from QMainWindow which would typically exist the whole time. However, you are generating a local copy in your constructor of chaild:
chaild::chaild(QWidget *parent) : QDialog(parent), ui(new Ui::chaild) { ui->setupUi(this); send st; connect(&st,SIGNAL(newTextEntered(QString)),this,SLOT(onNewTextEntered(QString))); }
Basically the send object is deleted immediately after the connect statement since the local object gets out of focus.
in send you do it basically the other way around:
void send::on_pushButton_clicked() { emit newTextEntered(ui->lineEdit->text()); // sending the signal with information chaild ss; // now you create the obejct where you expect to receive the signal ss.show(); ss.exec(); }
You are sending a signal to something that does not yet exist.
I think best is with the example code given above, you can start doing modifications and therefore find out more about the different strategies. Also other examples might help you.
-
please tell me how to pass the data from send form to chaild form
- How do you think about to take a bit more data modelling and corresponding presentation aspects into account?
- Would you like to clarify the amount of desired data exchange at these places?