How to handle QMessageBox when user click outside of MessageBox
-
hi, my problem was i have created a QMessageBox properly in raspberry pi but my problem was when i click on outside of the messagebox window application takes default as no. My need is when user click outside of the messageBox application must wont response for that event only the message box button should work at that time. Can you help me to do that. Below i have added my messageBox code
void MainWindow::on_pushButton_Shutdown_clicked() { qDebug()<<"entered shutdown message box"; QMessageBox msg(this); msg.setWindowFlags(Qt::Popup); msg.setStyleSheet("font: 75 20pt Liberation Serif ;"); QPixmap pix("/home/pi/git/FIA_Validation/FIA/notification.png"); auto newPixmap = pix.scaled(65, 65); msg.setIconPixmap(newPixmap); msg.setStyleSheet("font:16pt Arial;"); msg.setText("Are you sure you want to power off ?"); msg.setStandardButtons(QMessageBox::Ok | QMessageBox::No); msg.setDefaultButton(QMessageBox::NoButton); msg.setButtonText(QMessageBox::Ok , "Yes"); msg.setButtonText(QMessageBox::No , "Go back"); msg.setFixedWidth(500); if(msg.exec() == QMessageBox::Ok) { system("sudo poweroff"); //System Power Off Option } else { } } -
hi, my problem was i have created a QMessageBox properly in raspberry pi but my problem was when i click on outside of the messagebox window application takes default as no. My need is when user click outside of the messageBox application must wont response for that event only the message box button should work at that time. Can you help me to do that. Below i have added my messageBox code
void MainWindow::on_pushButton_Shutdown_clicked() { qDebug()<<"entered shutdown message box"; QMessageBox msg(this); msg.setWindowFlags(Qt::Popup); msg.setStyleSheet("font: 75 20pt Liberation Serif ;"); QPixmap pix("/home/pi/git/FIA_Validation/FIA/notification.png"); auto newPixmap = pix.scaled(65, 65); msg.setIconPixmap(newPixmap); msg.setStyleSheet("font:16pt Arial;"); msg.setText("Are you sure you want to power off ?"); msg.setStandardButtons(QMessageBox::Ok | QMessageBox::No); msg.setDefaultButton(QMessageBox::NoButton); msg.setButtonText(QMessageBox::Ok , "Yes"); msg.setButtonText(QMessageBox::No , "Go back"); msg.setFixedWidth(500); if(msg.exec() == QMessageBox::Ok) { system("sudo poweroff"); //System Power Off Option } else { } }@Mohan-Raj said in How to handle QMessageBox when user click outside of MessageBox:
My need is when user click outside of the messageBox application must wont response for that event only the message box button should work at that time.
That is what it normally does. It is because you added
msg.setWindowFlags(Qt::Popup);that it behaves the way you report. "Popup" means "click outside closes". So don't do that! -
i have a problem on removing that when i remove it, this shows with minimize and close options in frame when i click on that minimize it minimizes my whole application and also sometimes when i close the message box by using the frame option it also perform like that so please can you help me with any other options
-
i have a problem on removing that when i remove it, this shows with minimize and close options in frame when i click on that minimize it minimizes my whole application and also sometimes when i close the message box by using the frame option it also perform like that so please can you help me with any other options
@Mohan-Raj
I haven't looked into it, but there are flags for removing the "furniture" buttons like "minimize", "maximize" or "close". -
yes, you are right but if i use these flags messageBox was not opened or pop up
msg.setWindowFlags(Qt::WindowMinimizeButtonHint);
msg.setWindowFlags(Qt::WindowCloseButtonHint);@Mohan-Raj
Your code is wrong, in a couple of ways. You meant:msg.setWindowFlag(Qt::WindowMinimizeButtonHint, false); msg.setWindowFlag(Qt::WindowCloseButtonHint, false);Having said that, I don't actually find that suppresses the close button at least, Qt5 under Ubuntu Xorg. You'll have to play.
-
but the way you gave also not working. Okay that's fine I'll play further, Thanks for your cooperation
@Mohan-Raj said in How to handle QMessageBox when user click outside of MessageBox:
but the way you gave also not working.
I suggest my code is working! (Yours just plain did wrong thing.) You could verify by
qDebug() << msg.windowFlags()to check those two have indeed been switched off. Output before and after my code:QFlags<Qt::WindowType>(Dialog|MSWindowsFixedSizeDialogHint|WindowTitleHint|WindowSystemMenuHint|WindowCloseButtonHint) QFlags<Qt::WindowType>(Dialog|MSWindowsFixedSizeDialogHint|WindowTitleHint|WindowSystemMenuHint)Which shows the code works correctly, yet the message box still shows the "close" box. I don't know if that is a feature of the standard/native
QMessageBox. Looks like addingmsg.setWindowFlag(Qt::CustomizeWindowHint, true);allows it to be removed.
-
@Mohan-Raj said in How to handle QMessageBox when user click outside of MessageBox:
but the way you gave also not working.
I suggest my code is working! (Yours just plain did wrong thing.) You could verify by
qDebug() << msg.windowFlags()to check those two have indeed been switched off. Output before and after my code:QFlags<Qt::WindowType>(Dialog|MSWindowsFixedSizeDialogHint|WindowTitleHint|WindowSystemMenuHint|WindowCloseButtonHint) QFlags<Qt::WindowType>(Dialog|MSWindowsFixedSizeDialogHint|WindowTitleHint|WindowSystemMenuHint)Which shows the code works correctly, yet the message box still shows the "close" box. I don't know if that is a feature of the standard/native
QMessageBox. Looks like addingmsg.setWindowFlag(Qt::CustomizeWindowHint, true);allows it to be removed.
-
@JonB Yes you are right, its working properly when i see in Debug() but in application UI output the close ,minimize or not disabled or hidden working asusual.
UI output

Debug Output

@Mohan-Raj
Works for me under Qt5 and Xorg.
Obviously I don't have RPi. Are you perchance running under Wayland? If/assuming so, you probably won't be able to do much about changing stuff like this as Wayland likely doesn't allow it. Can you try under Xorg?On a separate matter or if this is the case. I do not know why you want to change this stuff. Leave message boxes etc. with whatever furniture is usual, that's standard for the windowing system. If you insist, try creating your own window with as little inbuilt stuff as possible, e.g. don't use
QMessageBoxorDialogstyle and see where you get to.