[SOLVED] QMessageBox. Setting custom size for two sequential windows crashes application
-
Hello all!
Since I've just started learning Qt, no wonder I have a lot of issues.
Currently I want to customize QMessageBox instances, which I call upon menu item selection. And first and foremost - dialog window size. Since setFixedSize() and any other direct size manipulations do not for some reason work, I use the workaround from "here":https://qt-project.org/forums/viewthread/24777/#114340. And here is my code:
@void FuzzyGUI::on_actionUpdates_triggered()
{
QMessageBox updateOptions;
updateOptions.setWindowTitle(tr("Update Checker"));
updateOptions.setIcon(QMessageBox::Question);
updateOptions.setText(tr("Are you sure there will be any updates?"));
updateOptions.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
updateOptions.setDefaultButton(QMessageBox::No);// Workaround for setting QMessageBox size // QSpacerItem* horizontalSpacer = new QSpacerItem(300, 100, QSizePolicy::Fixed, QSizePolicy::Fixed); QGridLayout* layout = (QGridLayout*)updateOptions.layout(); layout->addItem(horizontalSpacer, 0, 0, 0, 0); int userChoice = updateOptions.exec(); QMessageBox choiceConfirmation; choiceConfirmation.setWindowTitle(tr("Update Checker")); choiceConfirmation.setIcon(QMessageBox::Information); choiceConfirmation.setStandardButtons(QMessageBox::Ok); if (userChoice == QMessageBox::Yes) { choiceConfirmation.setText(tr("Wrong guess")); } else { choiceConfirmation.setText(tr("That is correct")); } // Workaround for setting QMessageBox size // layout = (QGridLayout*)choiceConfirmation.layout(); layout->addItem(horizontalSpacer, 0, 0, 0, 0); choiceConfirmation.exec();
}@
This works in term of providing requested dialog windows size, but once I click a button in the second window the application crashes... Everything works fine if I don't try to resize the second window.I would appreciate if someone could help me resolve the issue by either fixing the crash with existing workaround for window size or suggesting completely different approach via using probably other classes for sequential pop-up windows.
I would also be grateful if anybody could explain me how to get rid of window icon and control window background colour - I don't see in the "List of All Members":http://qt-project.org/doc/qt-5/qmessagebox-members.html (or simply do not understand) how it would be possible, while currently the dialog window is painted two colour: one for the text section and another for button section:
http://i067.radikal.ru/1402/3d/1ffd81bfe21c.jpg -
Solution for application crash so far: assign one more new QSpacerItem to horizontalSpacer prior to adding it into second modal window layout:
@void FuzzyGUI::on_actionUpdates_triggered()
{
QMessageBox updateOptions;
updateOptions.setWindowTitle(tr("Update Checker"));
updateOptions.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
updateOptions.setIcon(QMessageBox::Question);
updateOptions.setText(tr("Are you sure there will be any updates?"));
updateOptions.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
updateOptions.setDefaultButton(QMessageBox::No);// Workaround for setting QMessageBox size // QSpacerItem* horizontalSpacer = new QSpacerItem(300, 100, QSizePolicy::Fixed, QSizePolicy::Fixed); dynamic_cast<QGridLayout*>(updateOptions.layout())->addItem(horizontalSpacer, 0, 0, 0, 0); int userChoice = updateOptions.exec(); QMessageBox choiceConfirmation; QPixmap invisibleIcon = QPixmap(1, 1); choiceConfirmation.setWindowIcon(QIcon(invisibleIcon)); choiceConfirmation.setWindowTitle(tr("Update Checker")); choiceConfirmation.setIcon(QMessageBox::Information); choiceConfirmation.setStandardButtons(QMessageBox::Ok); if (userChoice == QMessageBox::Yes) { choiceConfirmation.setText(tr("Wrong guess")); } else { choiceConfirmation.setText(tr("That is correct")); } // Workaround for setting QMessageBox size // horizontalSpacer = new QSpacerItem(300, 100, QSizePolicy::Fixed, QSizePolicy::Fixed); dynamic_cast<QGridLayout*>(choiceConfirmation.layout())->addItem(horizontalSpacer, 0, 0, 0, 0); choiceConfirmation.exec();
}@
As shown in the code, I also dabbled with setWindowFlags() and setWindowIcon(). However, I did not succeed in getting rid of window icon: with setWindowFlags() I can get either window title bar with no window icon and window close button or with them both. With setWindowIcon() I can create 'invisible' icon, but the window icon placeholder in title bar remains...