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. QDialog Signals & Slots - How do I pass values back to the Main Form mid-function?

QDialog Signals & Slots - How do I pass values back to the Main Form mid-function?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.0k Views 2 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.
  • Q Offline
    Q Offline
    QtBit44
    wrote on last edited by
    #1

    I'm designing a QDialog as an enhanced QMessageBox. However, I am not sure how to handle the signals and slots correctly to pass values back to my Main Form mid-function. Slots only seem to pass values to a function during execution. Therefore, it seems that the only fix is to create global variables within the Main Form class. This would become unnecessarily complex and messy. I cannot use QSignalMapper (my variables are not restricted to integers and strings).

    Is there a way for a signal to pass values to a slot from one dialog to another in the middle of a function?

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

      Hi
      What you mean mid-function ?
      I mean , what function ?

      do you mean like ( pseudo code)

      mainwindow::DoDialog() {
      MyDialog d;
      d.exec();
      }

      and in MyDialog
      void Mydialog::DoTheStuff() {
      x = 100;
      emit MyNewValueToMain(x); // send signal to main
      more calc etc.
      }

      So Main gets some signal while the dialog is "up" ?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtBit44
        wrote on last edited by
        #3

        I'm trying to preserve values between slots or functions so that the values can be accessed from any function at any point in the Main Form class. I want the state of checkboxes and line edits in the dialog to be accessible to private variables, not global variables, in the Main Form class.

        Due to the design of the slots and signals, the Main Form class seems to be limited to:
        Creating invisible check boxes and line edits to temporarily store the returned values. This can unnecessarily complicate the forms.
        Creating global variables in the Main Form to carry the controls' states across every function.

        Is there a way to make this simpler, not more complex?

        1 Reply Last reply
        0
        • webzoidW Offline
          webzoidW Offline
          webzoid
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @QtBit44 I guess that instead of considering signals and slots to pass data from/to your dialog from main form, you may need to provide your dialog with getter/setters that allow you to set the values to display when your dialog is shown, and then once the user closes (accept) the dialog you ask it about the maybe new values. I have implemented something like this with a small dialog for server configuration, please imagine labels, line edits and checkbox here:

            address: .........
            port: .....
            https: X
            

            so there are setAddress(address), setPort(port), setHttps(true/false) and getters getAddress(), getPort() and isHttpsChecked() So some pseudo-code for using the dialog is as follows:

            mainwindow::DoDialog() {
                MyDialog d;
                d.setAddress("192.168.1.1");
                d.setPort(8080);
                d.setHttps(true);
                d.exec();
                // need to check if the user changed values 
                if (d.accepted()) {
                    if (d.getAddress != currentAddress) {
                         currentAddress = d.getAddress();
                    }
                    // check remaining values for changes
                }
            }
            

            Obviously, if your dialog got lots of values to set/get this approach may not be the best way to go. In that case I imagine breaking the dialog into smaller ones or considering a getter/setter for an object containing the values to display/change

            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
            1

            • Login

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