Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the values entered in the Main Window from a newly open Dialog ?
Forum Update on Monday, May 27th 2025

How to get the values entered in the Main Window from a newly open Dialog ?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 3.8k Views
  • 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.
  • L Offline
    L Offline
    Lazy93
    wrote on last edited by
    #1

    #signals #slots #QDialog

    JonBJ jsulmJ 2 Replies Last reply
    0
    • Pradeep KumarP Offline
      Pradeep KumarP Offline
      Pradeep Kumar
      wrote on last edited by
      #2

      Hi,

      Can u please ellaborate, u want values from QDialog is it? .
      If u want values from QDialog,

      1. Use connect statement in constructor.

      QObject::connect(Dialog_object, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));

      1. Define the slot where u want to get values from QDialog .

      void dialogIsFinished(int result)
      {
      if(result == QDialog::Accepted)
      {
      // Store the values from QDialog in variable.
      }
      else
      {
      // write the logic if u have canceled the QDialog operation
      }
      }

      Thanks,

      Pradeep Kumar
      Qt,QML Developer

      1 Reply Last reply
      4
      • L Lazy93

        #signals #slots #QDialog

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Lazy93
        Or, if you are using QDialog::exec(), after it returns (check return value) simply access variables via the QDialog instance, before it gets destroyed.

        1 Reply Last reply
        0
        • L Lazy93

          #signals #slots #QDialog

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Lazy93 What dialog is that exactly?
          And your question is really unclear: "How to get the values entered in the Main Window from a newly open Dialog".
          What values? Where are those entered (in main window or dialog)?

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

          1 Reply Last reply
          2
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Lazy93 this is a recurring question. Please do a search in the forum, take a look at this post for instance.

            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
            4
            • L Offline
              L Offline
              Lazy93
              wrote on last edited by
              #6

              Hello there guys, what I'm looking for is something like for example,

              At mainwindow, i will be asking the user to input a number of player, and then execute next form while carrying the input number from the mainwindow.

              something like

              1.Start,
              2.Mainwindow appear,
              3.Key in the number of player, // lets say i put an X
              4.Click OK/Proceed,
              5.A new dialog/form will be executed, // lets say its called Dialog1
              6.The number of player that has been input in the mainwindow is carried to that new form // I want to carry the value of X from the mainwindow to Dialog1

              jsulmJ 1 Reply Last reply
              0
              • L Lazy93

                Hello there guys, what I'm looking for is something like for example,

                At mainwindow, i will be asking the user to input a number of player, and then execute next form while carrying the input number from the mainwindow.

                something like

                1.Start,
                2.Mainwindow appear,
                3.Key in the number of player, // lets say i put an X
                4.Click OK/Proceed,
                5.A new dialog/form will be executed, // lets say its called Dialog1
                6.The number of player that has been input in the mainwindow is carried to that new form // I want to carry the value of X from the mainwindow to Dialog1

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #7

                @Lazy93 What is the problem?
                Create your own dialog/widget, add a constructor where you can pass this parameter (number of player) from main window to this dialog/window.

                class MyDialog: public QDialog
                {
                public:
                    MyDialog(int playerNumber, QWidget *parent = nullptr);
                }
                

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

                1 Reply Last reply
                5
                • L Offline
                  L Offline
                  Lazy93
                  wrote on last edited by
                  #8

                  Hi there, I tried that method but I get funny value on the new Dialog.

                  In the main window.h (header),

                  public:
                  explicit Home(QWidget *parent = 0);
                  ~Home();
                  long long pn;
                  private slots:
                  void on_lineEdit_textEdited(const QString &arg1);

                  at the mainwindow.cpp
                  void Home::on_lineEdit_textEdited(const QString &arg1)

                  {
                  pn = 0;

                  pn = arg1.toLongong();
                  

                  }
                  void Home::on_pushButton_clicked()
                  {
                  Dialog dialog;
                  dialog.setModal(true);
                  dialog.exec();
                  }

                  at dialog.h

                  public:
                  explicit Dialog(QWidget *parent= 0);
                  ~Dialog();
                  long long pn1;

                  at dialog.cpp

                  Home *home = new Home;

                  pn1 = home->pn;
                  ui->label->setText(QString ("%1").arg(pn1));

                  The problem is the number shown at label is not the same as the number i put in pn(at Home)

                  jsulmJ 1 Reply Last reply
                  0
                  • L Lazy93

                    Hi there, I tried that method but I get funny value on the new Dialog.

                    In the main window.h (header),

                    public:
                    explicit Home(QWidget *parent = 0);
                    ~Home();
                    long long pn;
                    private slots:
                    void on_lineEdit_textEdited(const QString &arg1);

                    at the mainwindow.cpp
                    void Home::on_lineEdit_textEdited(const QString &arg1)

                    {
                    pn = 0;

                    pn = arg1.toLongong();
                    

                    }
                    void Home::on_pushButton_clicked()
                    {
                    Dialog dialog;
                    dialog.setModal(true);
                    dialog.exec();
                    }

                    at dialog.h

                    public:
                    explicit Dialog(QWidget *parent= 0);
                    ~Dialog();
                    long long pn1;

                    at dialog.cpp

                    Home *home = new Home;

                    pn1 = home->pn;
                    ui->label->setText(QString ("%1").arg(pn1));

                    The problem is the number shown at label is not the same as the number i put in pn(at Home)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Lazy93 I don't understand the code fragments you posted.
                    You create an instance of Home, but you don't show it to the user - so how can the user change the value?!

                    Home *home = new Home;
                    pn1 = home->pn; // You will get the default value here as the dialog isn't shown and the user cannot change the value...
                    

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

                    1 Reply Last reply
                    1
                    • L Offline
                      L Offline
                      Lazy93
                      wrote on last edited by
                      #10

                      Hi what i want to know is how to pass the value entered from mainwindow to dialog ? I tried connect signal and slot. But that didnt work. Connect Signal and Slot only works if you already open Form A and then you open Form B, key in the data and pass it back to form A. What i want to do is to key in some data in form A and when I open Form B, i can get the data from Form A.

                      jsulmJ 1 Reply Last reply
                      0
                      • L Lazy93

                        Hi what i want to know is how to pass the value entered from mainwindow to dialog ? I tried connect signal and slot. But that didnt work. Connect Signal and Slot only works if you already open Form A and then you open Form B, key in the data and pass it back to form A. What i want to do is to key in some data in form A and when I open Form B, i can get the data from Form A.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Lazy93 said in How to get the values entered in the Main Window from a newly open Dialog ?:

                        what i want to know is how to pass the value entered from mainwindow to dialog ?

                        I already explained how (no need for signals/slots). And I pointed out (in my last post) that you're doing something wrong. Did you fix the bug?

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

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          Lazy93
                          wrote on last edited by
                          #12

                          class MyDialog: public QDialog
                          {
                          public:
                          MyDialog(int playerNumber, QWidget *parent = nullptr);
                          }

                          if i use this method, i cannot open my dialog with

                          Dialog *dialog = new Dialog;
                          dialog->exec();

                          as it says no matching function for call to 'Dialog::Dialog();

                          jsulmJ 1 Reply Last reply
                          0
                          • L Lazy93

                            class MyDialog: public QDialog
                            {
                            public:
                            MyDialog(int playerNumber, QWidget *parent = nullptr);
                            }

                            if i use this method, i cannot open my dialog with

                            Dialog *dialog = new Dialog;
                            dialog->exec();

                            as it says no matching function for call to 'Dialog::Dialog();

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Lazy93 Then fix the problem. Please take a closer look at the constructor...

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

                            1 Reply Last reply
                            2
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi
                              Just in case @jsulm's good hint do not ring any bells..

                              MyDialog(int playerNumber, QWidget *parent = nullptr);

                              Asks for INT and optional parent
                              but you give it nothing

                              Dialog *dialog = new Dialog;

                              so you need at least
                              Dialog *dialog = new Dialog(playerNumberVariable);

                              1 Reply Last reply
                              2
                              • L Offline
                                L Offline
                                Lazy93
                                wrote on last edited by
                                #15

                                Hi mrjj , How do i insert value into playernumber then ?

                                jsulmJ 1 Reply Last reply
                                0
                                • L Lazy93

                                  Hi mrjj , How do i insert value into playernumber then ?

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Lazy93 The user has to enter it, right? Then use this value.
                                  Here your own description:
                                  "
                                  1.Start,
                                  2.Mainwindow appear,
                                  3.Key in the number of player, // lets say i put an X
                                  4.Click OK/Proceed,
                                  5.A new dialog/form will be executed, // lets say its called Dialog1
                                  6.The number of player that has been input in the mainwindow is carried to that new form // I want to carry the value of X from the mainwindow to Dialog1
                                  "

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

                                  1 Reply Last reply
                                  1
                                  • L Offline
                                    L Offline
                                    Lazy93
                                    wrote on last edited by
                                    #17

                                    Yes, but how to use the playernumber in

                                    MyDialog(int playerNumber, QWidget *parent = nullptr); ?

                                    i think.....

                                    void Dialog::on_lineEdit_textEdited(const QString &arg1){

                                    playernumber = arg1.toInt ();? / / <<<< cannot be done right ?

                                    }

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • L Lazy93

                                      Yes, but how to use the playernumber in

                                      MyDialog(int playerNumber, QWidget *parent = nullptr); ?

                                      i think.....

                                      void Dialog::on_lineEdit_textEdited(const QString &arg1){

                                      playernumber = arg1.toInt ();? / / <<<< cannot be done right ?

                                      }

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Lazy93 said in How to get the values entered in the Main Window from a newly open Dialog ?:

                                      playernumber = arg1.toInt ();? / / <<<< cannot be done right ?

                                      it can be done, why not?

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

                                      1 Reply Last reply
                                      0
                                      • L Offline
                                        L Offline
                                        Lazy93
                                        wrote on last edited by
                                        #19

                                        Hi, How do i call the value from Dialog ?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • L Lazy93

                                          Hi, How do i call the value from Dialog ?

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by jsulm
                                          #20

                                          @Lazy93 Simply add a public getter method to your dialog:

                                          void Dialog::on_lineEdit_textEdited(const QString &arg1){
                                              _playernumber = arg1.toInt ();? / / <<<< cannot be done right ?
                                          }
                                          
                                          int Dialog::playerNumber()
                                          {
                                              return _playerNumber;
                                          }
                                          

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

                                          1 Reply Last reply
                                          2

                                          • Login

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