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. Beginner : getting the output of a Widget once closed ?
Qt 6.11 is out! See what's new in the release blog

Beginner : getting the output of a Widget once closed ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 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.
  • D Offline
    D Offline
    DonutMan
    wrote on last edited by DonutMan
    #1

    Hi,
    I'm really new in Qt and in GUI development.
    I use PyQt5 and I need to ask 3 times the user to select a value in a predefined set.

    Here's the class i developed to handle this task :

    class session_window(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(600,300)
            self.setWindowModality(Qt.ApplicationModal)
    
            d = QFormLayout()
       
            self.sat = QComboBox()
            self.sat.addItems(['A', 'B', 'C'])
            d.addRow(QLabel('Choose a value : '), self.sat)
            
            self.ok_button = QPushButton('Ok', clicked=self.next)
            d.addRow(None, self.ok_button)
            
            self.setLayout(d)
            self.show()
            
        def next(self):
            print('clicked !')
            my_list.append(self.sat.currentText())
            self.close()
    

    In the above example, my_list is a global variable that will store the successive results.

    And in my code I have something like :

    for k in range(3):
                print('- Asking session %d/3...' % (k+1))
                x = session_window()
    

    This does not work and I know that it is very bad design.. but i don't know how to handle that...
    Could you please tell me what kind of keywords I should read in the Qt doc to understand how to code this properly ?

    My example is very simplified but in reality :

    • some additional information will be needed at each iteration (so I will need to add additional widget in my class)
    • the number of iteration will be determined during the code execution and is not hard-coded

    Thanks a lot for your answer and sorry for the very basic question...

    BR

    Pierre

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

      Hi and welcome to devnet,

      Sounds like you could use a QWizard.

      That way you can create the widgets you need and show them as you need. Once the wizard is finished you can retrieve whatever field 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

      D 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        Sounds like you could use a QWizard.

        That way you can create the widgets you need and show them as you need. Once the wizard is finished you can retrieve whatever field you want.

        D Offline
        D Offline
        DonutMan
        wrote on last edited by
        #3

        @SGaist
        Hello, thank you very much for your answer !
        Finally I though adding setWindowModality(Qt.ApplicationModal) will stop the execution of code until the window returns but my reasoning was false.
        Indeed a QWizard object does exactly what I was expecting.
        I also tried playing with QDialog objects which behave in a similar way (ie : the code stop until the windows is closed).

        I was a bit surprised that these objects are still available once the self.close() is called. I somehow expected that the object will be destroyed just after being closed...

        Do I need to explicitly destroy them once I got the fields I wanted ?
        My application is very small but I want to adopt the good practices ^^

        Anyway, thanks a lot for your answer

        BR

        Pierre

        mrjjM SGaistS 2 Replies Last reply
        0
        • D DonutMan

          @SGaist
          Hello, thank you very much for your answer !
          Finally I though adding setWindowModality(Qt.ApplicationModal) will stop the execution of code until the window returns but my reasoning was false.
          Indeed a QWizard object does exactly what I was expecting.
          I also tried playing with QDialog objects which behave in a similar way (ie : the code stop until the windows is closed).

          I was a bit surprised that these objects are still available once the self.close() is called. I somehow expected that the object will be destroyed just after being closed...

          Do I need to explicitly destroy them once I got the fields I wanted ?
          My application is very small but I want to adopt the good practices ^^

          Anyway, thanks a lot for your answer

          BR

          Pierre

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DonutMan said in Beginner : getting the output of a Widget once closed ?:

          Do I need to explicitly destroy them once I got the fields I wanted ?

          Hi
          Well Qt has an owner system.
          https://doc.qt.io/qt-5/objecttrees.html

          which means, if you assign a parent, it will clean it up when the parent is deleted.

          However, you can manually clean it up after use, if you wish.
          but instead of a c++ delete then use
          https://doc.qt.io/qt-5/qobject.html#deleteLater

          D 1 Reply Last reply
          2
          • mrjjM mrjj

            @DonutMan said in Beginner : getting the output of a Widget once closed ?:

            Do I need to explicitly destroy them once I got the fields I wanted ?

            Hi
            Well Qt has an owner system.
            https://doc.qt.io/qt-5/objecttrees.html

            which means, if you assign a parent, it will clean it up when the parent is deleted.

            However, you can manually clean it up after use, if you wish.
            but instead of a c++ delete then use
            https://doc.qt.io/qt-5/qobject.html#deleteLater

            D Offline
            D Offline
            DonutMan
            wrote on last edited by
            #5

            @mrjj Hi, thanks a lot for your answer :)

            1 Reply Last reply
            0
            • D DonutMan

              @SGaist
              Hello, thank you very much for your answer !
              Finally I though adding setWindowModality(Qt.ApplicationModal) will stop the execution of code until the window returns but my reasoning was false.
              Indeed a QWizard object does exactly what I was expecting.
              I also tried playing with QDialog objects which behave in a similar way (ie : the code stop until the windows is closed).

              I was a bit surprised that these objects are still available once the self.close() is called. I somehow expected that the object will be destroyed just after being closed...

              Do I need to explicitly destroy them once I got the fields I wanted ?
              My application is very small but I want to adopt the good practices ^^

              Anyway, thanks a lot for your answer

              BR

              Pierre

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @DonutMan said in Beginner : getting the output of a Widget once closed ?:

              I was a bit surprised that these objects are still available once the self.close() is called. I somehow expected that the object will be destroyed just after being closed...

              Unless you set the deleteOnClose properly, they should not.

              Note that the Qt parent child system does not forbid you to do same memory handling.

              A child will live as long as it parent so if you create a lot of dialogs all the time without cleaning them, they will still eat your memory.

              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
              1

              • Login

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