Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Send Data from QDialog to QMainWindow
Forum Updated to NodeBB v4.3 + New Features

Send Data from QDialog to QMainWindow

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 4 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishbynature
    wrote on 7 Aug 2019, 07:35 last edited by
    #1

    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.

    J 1 Reply Last reply 7 Aug 2019, 08:03
    0
    • V vishbynature
      7 Aug 2019, 07:35

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Aug 2019, 08:03 last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • V Offline
        V Offline
        vishbynature
        wrote on 7 Aug 2019, 11:00 last edited by
        #3

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

        J J P 3 Replies Last reply 7 Aug 2019, 11:06
        0
        • V vishbynature
          7 Aug 2019, 11:00

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

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Aug 2019, 11:06 last edited by
          #4

          @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)));


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • V vishbynature
            7 Aug 2019, 11:00

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 7 Aug 2019, 11:12 last edited by
            #5

            @vishbynature Please read https://doc.qt.io/qt-5/signalsandslots.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • V vishbynature
              7 Aug 2019, 11:00

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

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 7 Aug 2019, 12:12 last edited by
              #6

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

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              J 1 Reply Last reply 7 Aug 2019, 12:25
              0
              • P Pablo J. Rogina
                7 Aug 2019, 12:12

                @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();
                ...
                }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 7 Aug 2019, 12:25 last edited by jsulm 8 Jul 2019, 12:26
                #7

                @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().

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 7 Aug 2019, 13:18 last edited by
                  #8

                  @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...

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0

                  1/8

                  7 Aug 2019, 07:35

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved