Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to pass string value from dialog to mainwindow
QtWS25 Last Chance

How to pass string value from dialog to mainwindow

Scheduled Pinned Locked Moved Unsolved Language Bindings
5 Posts 4 Posters 5.2k 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.
  • D Offline
    D Offline
    dancaer69
    wrote on last edited by
    #1

    Hello,
    I'm new to python and qt, but in the past I did some programming in java and android. I created before a lot of years an application in java, which I use still and I'm trying to recreated now with python and pyqt5.
    I'm using eric IDE and I have created 2 forms, a mainwindow and a dialog so far.
    eric generates .py files for the forms, so I have separate files for the mainwindow form and the dialog form. I have already imported the dialog form to the mainwindow form successfully and I can open the dialog form by click on a button.
    I have some radioboxes in dialog form and I would like when I click on one of them to update a label' s text on mainwindow form. I have already generated code for onClick action for all the radioboxes and store the value to a string when any of them clicked. But I don't know how to also set this value as mainwinodw' s label text. I searched about this and I found some answers about store to a global variable. I tried that, but I get error when I'm trying to import the mainwindow form's class to the dialog form file.

    The debugged program raised the exception unhandled ImportError
    "cannot import name 'MainWindow
    

    So I cannot connect the dialog form to mainwindow form to somehow pass the data.
    I have both of forms in an ui subdirectory and I tried all these:

    from .mainwindow import MainWindow
    from ui.mainwindow import MainWindow
    from mainwindow import MainWindow
    

    but always I get the above error when I'm trying to start the application.
    So the main problem now is how to connect the dialog to the mainwindow and then how to pass the data.

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

      Hi and welcome to devnet,

      Don't use global variable for that. You can either use signals and slots or simply have your dialog provide getters to retrieve the values you want.

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

      1 Reply Last reply
      4
      • D dancaer69

        Hello,
        I'm new to python and qt, but in the past I did some programming in java and android. I created before a lot of years an application in java, which I use still and I'm trying to recreated now with python and pyqt5.
        I'm using eric IDE and I have created 2 forms, a mainwindow and a dialog so far.
        eric generates .py files for the forms, so I have separate files for the mainwindow form and the dialog form. I have already imported the dialog form to the mainwindow form successfully and I can open the dialog form by click on a button.
        I have some radioboxes in dialog form and I would like when I click on one of them to update a label' s text on mainwindow form. I have already generated code for onClick action for all the radioboxes and store the value to a string when any of them clicked. But I don't know how to also set this value as mainwinodw' s label text. I searched about this and I found some answers about store to a global variable. I tried that, but I get error when I'm trying to import the mainwindow form's class to the dialog form file.

        The debugged program raised the exception unhandled ImportError
        "cannot import name 'MainWindow
        

        So I cannot connect the dialog form to mainwindow form to somehow pass the data.
        I have both of forms in an ui subdirectory and I tried all these:

        from .mainwindow import MainWindow
        from ui.mainwindow import MainWindow
        from mainwindow import MainWindow
        

        but always I get the above error when I'm trying to start the application.
        So the main problem now is how to connect the dialog to the mainwindow and then how to pass the data.

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

        @dancaer69
        @SGaist is (of course!) right. To expand on what he says:

        • "use signals and slots": in the dialog, use emit() to raise a signal once the value has been set. In your main window module, use connect() to register a slot defined there which will receive the string passed in the signal by the dialog.

        • "have your dialog provide getters to retrieve the values you want": from main window create your dialog instance, then execute it, then while the dialog variable is still in scope you can call functions in it to retrieve values:

        dlg = QDialog(self)
        if dlg.exec():
            value = dlg.someFunction()
        

        In this case, do not set the Qt "auto delete dialog object after executing" flag (it is not normally switched on).

        Either method is acceptable, with pros & cons. Both are preferable to using a "global variable" (which you will find pretty difficult/non-obvious in Python anyway).

        1 Reply Last reply
        2
        • D Offline
          D Offline
          dobiz22
          wrote on last edited by
          #4

          This answer was very helpful, thank you for taking the time!

          Just curious... is there a downside to accessing the variable directly rather than using a getter function (which I know are common in Java and C, but less used in Python).

          dlg = QDialog(self)
          if dlg.exec():
              value = dlg.some_variable_in_qdialog
          
          JonBJ 1 Reply Last reply
          0
          • D dobiz22

            This answer was very helpful, thank you for taking the time!

            Just curious... is there a downside to accessing the variable directly rather than using a getter function (which I know are common in Java and C, but less used in Python).

            dlg = QDialog(self)
            if dlg.exec():
                value = dlg.some_variable_in_qdialog
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @dobiz22
            Just that it is better "style" to access via getters & setters. Hides some of the internals, allows a bit of extra code to be put into the getter/setter if required (e.g. validation), you can place a debugger breakpoint on them, that sort of thing.

            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