Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Help sending data from dialog to main window
QtWS25 Last Chance

Help sending data from dialog to main window

Scheduled Pinned Locked Moved Unsolved Brainstorm
13 Posts 5 Posters 616 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.
  • C Offline
    C Offline
    Cantona
    wrote on last edited by
    #1

    Hello everyone, am new to the forum.
    Ok so I have two Windows in my project, main window and a dialog window called login. This login window just have a line edit called username and a push button called loginbtn. What I want is to have a QString variable called username in the main window that is equal to an empty string, so when I provide a username in the dialog window and click the login button, the text in the line edit get assigned to the empty username variable in the main window. Thank you.

    JonBJ alexjordan_nowA 2 Replies Last reply
    1
    • C Cantona

      Hello everyone, am new to the forum.
      Ok so I have two Windows in my project, main window and a dialog window called login. This login window just have a line edit called username and a push button called loginbtn. What I want is to have a QString variable called username in the main window that is equal to an empty string, so when I provide a username in the dialog window and click the login button, the text in the line edit get assigned to the empty username variable in the main window. Thank you.

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

      @Cantona
      Provide a public getter and a setter for the text in the line edit in the dialog (subclass QDialog). Include the dialog declaration via header file into main window, so it knows about it. Optionally set the value via setter to something (e.g. empty string) prior to invoking QDialog::exec(). Upon return from that, check result to see whether user went "OK" or "Cancel". If "OK" assign to variable in main window via getter in dialog.

      In other words, just use public setters & getters to access text value in dialog from main window.

      You could also use signal from dialog to pass username to slot in main window, but I don't see a need here, unless you prefer that to getter/setter. Getter/setter can be expanded more easily, e.g. if you decide to pass a password back as well as a username.

      If this is a "play" project you could just use QInputDialog::getText() to get a single string returned from a dialog instead. But that is limited to just that.

      C 1 Reply Last reply
      1
      • C Offline
        C Offline
        Cantona
        wrote on last edited by Cantona
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • JonBJ JonB

          @Cantona
          Provide a public getter and a setter for the text in the line edit in the dialog (subclass QDialog). Include the dialog declaration via header file into main window, so it knows about it. Optionally set the value via setter to something (e.g. empty string) prior to invoking QDialog::exec(). Upon return from that, check result to see whether user went "OK" or "Cancel". If "OK" assign to variable in main window via getter in dialog.

          In other words, just use public setters & getters to access text value in dialog from main window.

          You could also use signal from dialog to pass username to slot in main window, but I don't see a need here, unless you prefer that to getter/setter. Getter/setter can be expanded more easily, e.g. if you decide to pass a password back as well as a username.

          If this is a "play" project you could just use QInputDialog::getText() to get a single string returned from a dialog instead. But that is limited to just that.

          C Offline
          C Offline
          Cantona
          wrote on last edited by
          #4

          @JonB

          Please can you show a quick guide on how to use signals and slot for this

          JonBJ 1 Reply Last reply
          0
          • C Cantona

            @JonB

            Please can you show a quick guide on how to use signals and slot for this

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

            @Cantona Why, given that I said of signals/slots "but I don't see a need here"? Getter/setter is simpler and better here.

            C 1 Reply Last reply
            0
            • JonBJ JonB

              @Cantona Why, given that I said of signals/slots "but I don't see a need here"? Getter/setter is simpler and better here.

              C Offline
              C Offline
              Cantona
              wrote on last edited by
              #6

              @JonB

              I have never used getter/setter in qt so I don't know how to go about it

              JonBJ 1 Reply Last reply
              0
              • C Cantona

                @JonB

                I have never used getter/setter in qt so I don't know how to go about it

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

                @Cantona
                You have used it many times, Qt uses this pattern for all its class variables. It's just simple C++ (or Python):

                // .h file
                class UsernameDialog : public QDialog
                {
                public:
                    const QString &username() const;    // "getter" for username
                    void setUsername(const QString &username);    // "setter" for username, probably not used for this case
                }
                
                // .cpp file
                const QString &UsernameDialog::username() const
                {
                    return ui->lineEdit.text();
                }
                void UsernameDialog::setUsername(const QString &username)
                {
                    ui->lineEdit.setText(username);
                }
                
                // mainwindow cpp file
                QString username;
                UsernameDialog *usernameDialog = new UsernameDialog;
                if (usernameDialog->exec())
                    username = usernameDialog->username();
                
                C 1 Reply Last reply
                1
                • JonBJ JonB

                  @Cantona
                  You have used it many times, Qt uses this pattern for all its class variables. It's just simple C++ (or Python):

                  // .h file
                  class UsernameDialog : public QDialog
                  {
                  public:
                      const QString &username() const;    // "getter" for username
                      void setUsername(const QString &username);    // "setter" for username, probably not used for this case
                  }
                  
                  // .cpp file
                  const QString &UsernameDialog::username() const
                  {
                      return ui->lineEdit.text();
                  }
                  void UsernameDialog::setUsername(const QString &username)
                  {
                      ui->lineEdit.setText(username);
                  }
                  
                  // mainwindow cpp file
                  QString username;
                  UsernameDialog *usernameDialog = new UsernameDialog;
                  if (usernameDialog->exec())
                      username = usernameDialog->username();
                  
                  C Offline
                  C Offline
                  Cantona
                  wrote on last edited by
                  #8

                  @JonB

                  Sorry I couldn't get this to work

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    What exactly did not work now ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    C 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      What exactly did not work now ?

                      C Offline
                      C Offline
                      Cantona
                      wrote on last edited by
                      #10

                      @SGaist

                      Everything I guess. D variable username still is not getting any data assigned to it

                      Pl45m4P 1 Reply Last reply
                      0
                      • C Cantona

                        @SGaist

                        Everything I guess. D variable username still is not getting any data assigned to it

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by
                        #11

                        @Cantona said in Help sending data from dialog to main window:

                        variable username still is not getting any data assigned to it

                        Unpleasant answer but no secret or surprise:
                        You need to know C++ if you want to get somewhere with Qt :)
                        Learn the basics of C++ and OOP and you will understand better and learn faster how to work with Qt.

                        @JonB said in Help sending data from dialog to main window:

                        // mainwindow cpp file
                        QString username;
                        UsernameDialog *usernameDialog = new UsernameDialog;
                        if (usernameDialog->exec())
                            username = usernameDialog->username();
                        

                        This answer by @JonB is basically everything you need to do to return your variable from your dialog and assign it to something within your QMainWindow class.


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        2
                        • C Cantona

                          Hello everyone, am new to the forum.
                          Ok so I have two Windows in my project, main window and a dialog window called login. This login window just have a line edit called username and a push button called loginbtn. What I want is to have a QString variable called username in the main window that is equal to an empty string, so when I provide a username in the dialog window and click the login button, the text in the line edit get assigned to the empty username variable in the main window. Thank you.

                          alexjordan_nowA Offline
                          alexjordan_nowA Offline
                          alexjordan_now
                          wrote on last edited by
                          #12

                          @Cantona You can achieve this by emitting a signal from the login dialog and connecting it to a slot in the main window. In LoginDialog, emit a signal like emit sendUsername(username); when the button is clicked. Then, in MainWindow, connect it with connect(loginDialog, &LoginDialog::sendUsername, this, &MainWindow::setUsername);.

                          Hope these steps help you out!

                          Pl45m4P 1 Reply Last reply
                          0
                          • alexjordan_nowA alexjordan_now

                            @Cantona You can achieve this by emitting a signal from the login dialog and connecting it to a slot in the main window. In LoginDialog, emit a signal like emit sendUsername(username); when the button is clicked. Then, in MainWindow, connect it with connect(loginDialog, &LoginDialog::sendUsername, this, &MainWindow::setUsername);.

                            Hope these steps help you out!

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by
                            #13

                            @alexjordan_now said in Help sending data from dialog to main window:

                            You can achieve this by emitting a signal from the login dialog and connecting it to a slot in the main window

                            That's one way that was also already mentioned before :)
                            Though I think if you want to return one value only, setting up a signal/slot connection is more "work" than just returning the value directly to the caller.


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            1 Reply Last reply
                            1

                            • Login

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