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.5k 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 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.

    jsulmJ 1 Reply Last reply
    0
    • V vishbynature

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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 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.HilkJ jsulmJ Pablo J. RoginaP 3 Replies Last reply
        0
        • V vishbynature

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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

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

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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

              jsulmJ 1 Reply Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

                @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();
                ...
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #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
                • Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 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

                  • Login

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