@SGaist said in Why does the Irrlicht window turn black when opening a QMessageBox?:
@Chris-Kawa's suggestion is the right one. In your case avoid using the static methods, create your own message box and call open rather than exec.
Thanks, @Chris-Kawa and @SGaist. I did as you suggested and I've written my own version of QMessageBox::information that works with Irrlicht:
#include <QMessageBox>
#include <functional>
void showMessageBox(std::function<void(QMessageBox::StandardButton)> lambda, QWidget *parent, const QString &title, const QString &text, QMessageBox::Icon icon = QMessageBox::Information, QFlags<QMessageBox::StandardButton> buttons = QMessageBox::Ok){
QMessageBox *messageBox = new QMessageBox(icon, title, text, buttons, parent);
messageBox->open(NULL, NULL);
QObject::connect(messageBox, &QMessageBox::finished, [messageBox, lambda](){
if(messageBox->clickedButton() == messageBox->button(QMessageBox::Ok)){
lambda(QMessageBox::Ok);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Open)){
lambda(QMessageBox::Open);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Save)){
lambda(QMessageBox::Save);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Cancel)){
lambda(QMessageBox::Cancel);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Close)){
lambda(QMessageBox::Close);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Discard)){
lambda(QMessageBox::Discard);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Apply)){
lambda(QMessageBox::Apply);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Reset)){
lambda(QMessageBox::Reset);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::RestoreDefaults)){
lambda(QMessageBox::RestoreDefaults);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Help)){
lambda(QMessageBox::Help);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::SaveAll)){
lambda(QMessageBox::SaveAll);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Yes)){
lambda(QMessageBox::Yes);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::YesToAll)){
lambda(QMessageBox::YesToAll);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::No)){
lambda(QMessageBox::No);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::NoToAll)){
lambda(QMessageBox::NoToAll);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Abort)){
lambda(QMessageBox::Abort);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Retry)){
lambda(QMessageBox::Retry);
}
else if(messageBox->clickedButton() == messageBox->button(QMessageBox::Ignore)){
lambda(QMessageBox::Ignore);
}
else{
lambda(QMessageBox::NoButton);
}
});
}
How to use it:
The first parameter is a lambda which is executed when the dialog box gets closed. It takes one parameter, the button on which the user clicked.
The second parameter is the parent window, it works like the first parameter of QMessageBox::information.
The third parameter is the title of the window.
The fourth parameter is the message.
The fifth parameter is the icon of the message box. Possible values: QMessageBox::NoIcon, QMessageBox::Question, QMessageBox::Information, QMessageBox::Warning, QMessageBox::Critical.
The sixth parameter is the buttons of the dialog box, it works like the fourth parameter of QMessageBox::information.
Example:
showMessageBox([](QMessageBox::StandardButton result){
if(result == QMessageBox::Yes){
showMessageBox([](QMessageBox::StandardButton){}, myWidget, "Hello", "Hello world!");
}
}, myWidget, "Title", "Do you want to open a new dialog box?", QMessageBox::Question, QMessageBox::Yes | QMessageBox::No);
The above code asks the user if he wants to open a new dialog box. If the user clicks on Yes, it opens a dialog box saying "Hello World!", otherwise, nothing happens. myWidget is the program's main window.