Qt 6.11 is out! See what's new in the release
blog
QDialog crashing on importing QLayout
-
Hi. I have the following code:
QString CCCFPMenuBar::passwordReader(){ //Create a layout involving a label, password, a line edit and submit button QVBoxLayout layout(this); QLabel passwordLabel("Password:"); QPushButton submitButton; QLineEdit password; //Create a box with that layout QDialog passwordDialog(this); layout.addWidget(&passwordLabel); layout.addWidget(&submitButton); layout.addWidget(&password); qDebug() << "83"; passwordDialog.setLayout(&layout); //passwordDialog.exec(); return "AHHHHH";The code crashes at
passwordDialog.setLayout(&layout);but I am unsure why. My syntax from the QLayout seems accurate based on my understanding, and the QLayout doesn't seem (again, in my understanding) to be invalid for QDialog.Thanks for your time and please let me know if more information is required.
-
A widget takes ownership of a layout and its child widgets, meaning it deletes them when it is itself deleted (or goes out of scope when created on the stack). This means you shouldn't create the layout or the widgets on the stack, as you get a double delete problem - once by the parent and once by going out of scope.