Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Send Data from QDialog to QMainWindow



  • Hello,
    I Have been trying to send int data from Qdialog to MainWindow of my project. So far not been able to achieve this I used Signals and slots for this
    I would emit a signal with int value in it and in MainWindow I will connect this signal to a slot to get data but doesn't seem to work.
    for more clarity: MainWindow opens another QDialog by .exec() method and then goes on to open other QDialog by same method and at the end is the QDialog from which i want to send data to MainWindow().
    Regards.


  • Qt Champions 2018

    @vishbynature said in Send Data from QDialog to QMainWindow:

    MainWindow opens another QDialog by .exec() method and then goes on to open other QDialog by same method and at the end is the QDialog from which i want to send data to MainWindow()

    Are you aware that exec() is a blocking call? Everything after exec() will be executed after the dialog terminates.
    Also, show your code, without code how can anybody tell you what is wrong?



  • @jsulm

    MainWindow code as follows:

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    connect(&sc,SIGNAL(send()),this,SLOT(print()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::print(int var)
    {
    ui->lineEdit_2->setText(QString::number(var));
    }

    dialog 4 code as follows:

    int l=69;
    sec::sec(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::sec)
    {
    ui->setupUi(this);
    }

    sec::~sec()
    {
    delete ui;
    }

    void sec::on_pushButton_clicked()
    {
    emit send(l);
    this->close();
    }


  • Moderators

    @vishbynature said in Send Data from QDialog to QMainWindow:
    hi

    connect(&sc,SIGNAL(send()),this,SLOT(print()));

    this connect is, wrong, your send signal and your print slot both have a int as argument send() and print() do not exist, and you should actually get a runtime error/warning in your console when you run that program.

    connect(&sc,SIGNAL(send(int)),this,SLOT(print(int)));


  • Qt Champions 2018



  • @vishbynature you can control the life span of your dialog from main window, so you can:

    1. create a dialog object in main window
    2. display the dialog for the user to enter all the required data
    3. once user closes the dialog, control returns to main window
    4. main window asks the dialog (with getter methods) for the desired data

    pseudo-code:

    MainWindow::methodX() {
       MyDialog diag;
       diag.show()                        // when user closes dialog, object will still be accesible from mainwindow
       diag.getDataX();
       diag.getDataY();
    ...
    }
    

  • Qt Champions 2018

    @pablo-j-rogina said in Send Data from QDialog to QMainWindow:

    MainWindow::methodX() {
    MyDialog diag;
    diag.show() // when user closes dialog, object will still be accesible from mainwindow
    diag.getDataX();
    diag.getDataY();
    ...
    }

    Since show() does not block you would call getData*() just after starting the dialog before the user enters something and closes it. exec() would work though.

    "// when user closes dialog, object will still be accesible from mainwindow" - not in your example as diag is local variable and does not exist outside of methodX().



  • @jsulm two good catches!!

    exec() would work though

    Yes, the dialog's method that keeps the dialog alive...

    still be accesible from mainwindow

    I should have said from MainWindow::methodX...


Log in to reply