Beginner : getting the output of a Widget once closed ?
-
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
-
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.
@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
-
@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
@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.htmlwhich 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 -
@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.htmlwhich 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 -
@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
@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.