PDA

View Full Version : exec QMessageBox



saman_artorious
7th May 2013, 07:24
is there anyway not to lose focus of QMessageBox to Mainwindow unless it is closed first?



QMessageBox msgBox;
msgBox.setText(trUtf8(""));
msgBox.setInformativeText"");
QAbstractButton *myYesButton = msgBox.addButton(trUtf8(" "), QMessageBox::YesRole);
QAbstractButton *myNoButton = msgBox.addButton(trUtf8(" "),QMessageBox::NoRole);
msgBox.setIcon(QMessageBox::Question);

msgBox.exec(); //???

if(msgBox.clickedButton() == myNoButton)
{
qDebug() << "no";
return;
}
else
{
system("sudo shutdown -hf now");
}

ChrisW67
7th May 2013, 07:51
You are opening the QMessageBox as a modal dialog. It will take the focus for the entire application until it is closed. That seems to be exactly what you are asking for.

If you want a modeless dialog call show() instead of exec() and be to respond to button clicks asynchronously in connected slots.

saman_artorious
7th May 2013, 08:40
You are opening the QMessageBox as a modal dialog. It will take the focus for the entire application until it is closed. That seems to be exactly what you are asking for.


no, when I click the mainwindow, it loses focus and goes to background. Besides, it is run as a separate program.

but in the case

reply = QMessageBox::question(this, tr(" "), " ", QMessageBox::Yes | QMessageBox::No);

it is run in the same MainWindow program and does not lose focus when mainwindow is clicked, unless we choose yes or no.

I cannot use the second second because I need to translate Yes and No.

ChrisW67
7th May 2013, 08:53
Besides, it is run as a separate program.
What did you expect to happen... The second program to magically know which other program to block and block it?

saman_artorious
7th May 2013, 09:10
What did you expect to happen... The second program to magically know which other program to block and block it?

yes, I understand.

I am looking for a way to make it look like


reply = QMessageBox::question(this, tr(" "), " ", QMessageBox::Yes | QMessageBox::No);

ChrisW67
7th May 2013, 09:18
I have no idea why you would have program 1 launch program 2 just to display a dialog when there is a clear an obvious way to use the dialog inline... but it's your party.

To achieve what you are asking you would need the launching program to call setDisabled(true) on its top-level window, launch the second program, and wait from the termination of the called program before calling setEnabled(true). The second program should be written to be always on-top. The Qt::WindowStaysOnTopHint window flag is one way you might be able to do this.

saman_artorious
7th May 2013, 09:58
I have no idea why you would have program 1 launch program 2 just to display a dialog when there is a clear an obvious way to use the dialog inline... but it's your party.

To achieve what you are asking you would need the launching program to call setDisabled(true) on its top-level window, launch the second program, and wait from the termination of the called program before calling setEnabled(true). The second program should be written to be always on-top. The Qt::WindowStaysOnTopHint window flag is one way you might be able to do this.

thanks for the way you mentioned. about the first 2 lines, I do not want the first program to launch the second. But, unfortunately, the way the code behaves is like that.

saman_artorious
8th May 2013, 07:26
i created a custom dialog with class. initialized new dialog instance in mainwindow constructor. when button clicked, I show the dialog. before that I disable the mainwindow. inside new dialog class before closing it, I signal mainwindow to get enabled.
the problem is when mainwindow sets to disabled, the buttons inside the custom dialog get disabled as well! and the close button top-left side of custom dialog onlyworks.

ChrisW67
8th May 2013, 08:15
Now your dialog is part of the same application! Make up your mind!

My first response applies and you don't need to do anything special: a modal dialog will block all input to the remainder of the application it belongs to. If the dialog has a parent then it will stay on top of that parent.