Here's what happens:
- I start example_12 from in a terminal window.
- The main window appears with two push buttons and two edit fields.
- I press the push button marked "Item" and a dialog box titled "Item" appears containing a combo box with the items "Spring", "Summer", "Fall" and Winter"
- I pick an item from the combo box or I just ignore it.
- I leave the dialog by pressing "OK", "Cancel" or the red button with the cross in the left corner.
- The dialog box disappears together with the main window.
- The terminal shows the message "Segmentation fault: 11".
void MainWindow::setItem()
{
...QStringList itemList;
...itemList << "Spring" << "Summer" << "Fall" << "Winter";
...bool ok;
...QString data = QInputDialog::getItem(this, "Item",
........"Select a Season:", itemList, 0, false, &ok);
...if (ok && ! data.isEmpty()) {
......item_text->setText(data);
...}
}
The expected behaviour is that the dialog box closes and that the selected text (alternatively the default selection if I don't pick anything) is shown in the edit field to the right of the push button.
I looked up the source for QInputDialog::getItem and that prompted me to try the following:
void MainWindow::setItem()
{
...QStringList itemList {"Spring", "Summer", "Fall", "Winter"};
...bool ok;
...QInputDialog *dialog = new QInputDialog(this);
...dialog->setComboBoxEditable(false);
...dialog->setComboBoxItems (itemList);
...dialog->setLabelText("Select a Season:");
...dialog->setWindowTitle("Item");
...const int ret = dialog->exec();
...if (ret == QInputDialog::Accepted) {
......QString data = dialog->textValue();
......item_text->setText(data);
...}
...// NB! Notice the following line is commented out. If not the program crashes.
...// delete dialog;
}
This code gives the expected behaviour. It only works, however, if I don´t release the QInputDialog object with "delete dialog". So it seems something happens when the object is deleted - either because it (the QInputDialog object in the static ::getItem method) goes out of scope like in the original example_12, or because it is released with delete like I did in my code.