How to pass values from 2 differents dialogs to 1 dialog?
-
I want to add a new Dialog (Dialog2). I want that Dialog0 and the new Dialog2 can pass values to Dialog1. I only can pass values from one Dialog to another one, i mean one-to-one but no two-to-one. ¿How can i do it?
Dialog0.cpp (only a fragment)
void Dialog0::on_pushButton_clicked() { Dialog1 *v = new Dialog1(this, var); close(); v->show(); }
Dialog0.h
#ifndef DIALOG0_H #define DIALOG0_H #include <QDialog> #include <dialog1.h> namespace Ui { class Dialog0; } class Dialog0 : public QDialog { Q_OBJECT public: explicit Dialog0(QWidget *parent = 0); ~Dialog0(); int var; private slots: void on_pushButton_clicked(); private: Ui::Dialog0 *ui; }; #endif // DIALOG0_H
Dialog1.cpp
#include "dialog1.h" #include "ui_dialog1.h" #include <dialog1.h> Dialog1::Dialog1(QWidget *parent, int var) : QDialog(parent), ui(new Ui::Dialog1), var(var) { ui->setupUi(this); ... } Dialog1::~Dialog1() { delete ui; } void Dialog1::on_pushButton_clicked() { ... }
Dialog1.h
#ifndef DIALOG1_H #define DIALOG1_H #include <QDialog> namespace Ui { class Dialog1; } class Dialog1 : public QDialog { Q_OBJECT public: explicit Dialog1(QWidget *parent = 0); Dialog1(QWidget *parent = 0, int var = 0); ~Dialog1(); QString curso; int numero; int cont; private slots: void on_pushButton_clicked(); private: int var; Ui::Dialog1 *ui; }; #endif // DIALOG1_H
-
@Eduardo12l Hi, friend, welcome.
Where is your
QDialog2
new ? Can you tell me or show some code forQDialog2
?From your code snippet, I only know you
new QDialog1
inDialog0::button_click_function
. -
@joeQ Hi ^^ . This could be my Dialog2 but i dont know how to pass "nu" to Dialog1
#include "dialog2.h" #include "ui_dialog2.h" Dialog2::Dialog2(QWidget *parent): QDialog(parent), ui(new Ui::Dialog2) { ui->setupUi(this); } Dialog2::~Dialog2() { delete ui; } void Dialog2::on_pushButton_2_clicked() { ... Dialog1 *a = new Dialog1(this, nu); close(); a->show(); }
-
Hi, from your code, you did it.
Dialog1 *a = new Dialog1(this, nu); ///< you pass un to Dialog1
can you show dialog2.h file ?
I want to know where is you new dialog2 object ?
-
@Eduardo12l Just use signals/slots, you can connect as many signal sender to one receiver as you want (it is n:m).
-
@joeQ It has nothing because i dont know what to do
#ifndef VENTANAS2_H #define VENTANAS2_H #include <QDialog> namespace Ui { class Ventanas2; } class Ventanas2 : public QDialog { Q_OBJECT public: explicit Ventanas2(QWidget *parent = 0); ~Ventanas2(); private: Ui::Ventanas2 *ui; }; #endif // VENTANAS2_H
-
@Eduardo12l You connect a signal from Dialog0 to Dialog1 and you connect a signal from Dialog2 to Dialog1, that's all. You pass data as signal parameter:
class SignalSender: public QObject { Q_OBJECT signals: void signal1(const QString &someData); }; class SignalReceiver: public QObject { Q_OBJECT public slots: void doSomething(const QString &someData); } SignalSender *sender = new SignalSender(); SignalReceiver *receiver = new SignalReceiver(); connect(sender, SIGNAL(signal1(QString)), receiver, SLOT(doSomething(QString))); // then somewhere in SignalSender: QString data = "some data"; emit signal1(data);
Everything is described here: http://doc.qt.io/qt-5.9/signalsandslots.html