PDA

View Full Version : WindowCloseButton



wirasto
7th January 2010, 18:21
Why if I use setDetailedText for QMessageBox then WindowCloseButton disabled ? How to enable it ? I tried this, but not work..



msg.setDetailedText("Cape Deh...");
msg.setWindowFlags(msg.windowFlags() | Qt::WindowCloseButtonHint);

wysota
7th January 2010, 18:22
Please provide a minimal compilable example reproducing the problem.

wirasto
7th January 2010, 18:33
QMessageBox msg(this);
msg.setIcon(QMessageBox::Critical);
msg.setWindowTitle("Error");
msg.setText(tr("Sambungan ke database gagal!"));
msg.setDetailedText("Cape Deh...");
msg.setWindowFlags(msg.windowFlags() | Qt::WindowCloseButtonHint);
msg.exec();

faldzip
7th January 2010, 20:39
I found that there is something like detectedEscapeButton in QMessageBox and setting detailed description causes situation where QMessageBox can't determine detectedEscapeButton so this:


void QMessageBox::closeEvent(QCloseEvent *e)
{
Q_D(QMessageBox);
if (!d->detectedEscapeButton) {
e->ignore();
return;
}
QDialog::closeEvent(e);
d->clickedButton = d->detectedEscapeButton;
setResult(d->execReturnCode(d->detectedEscapeButton));
}

means that with detectedEscapeButton set to 0 close event is always ignored.
In detail: escape button detection is performed on show event. If there is no explicitly set escape button (setEscapeButton) then it is detected which means that QMessageBox search for any button with RejectRole or NoRole or if there is only one button then this one becomes escape button. Adding detailed text adds second button so there is no proper rule to set detectedEscapeButton.
I found the solution and it is adding yout own button with RejectRole or NoRole but remember that you will get diffrent return code from exec() in this code:


QMessageBox msg;
msg.setIcon(QMessageBox::Critical);
msg.setWindowTitle("Error");
msg.setText("Sambungan ke database gagal!");
msg.setDetailedText("Cape Deh...");
msg.addButton("OK", QMessageBox::RejectRole);
qDebug("%d", msg.exec()); // here now 0 is returned, with default created OK button it was 1024 (QMessageBox::Ok)

wysota
7th January 2010, 22:13
Why not use setEscapeButton() with the button that already is in the message box?

faldzip
7th January 2010, 22:31
Why not use setEscapeButton() with the button that already is in the message box?
I wanted to do so but as I checked QMessageBox::buttons() returnes empty list when used before exec() or show() which is I think the result of this QMessageBox::showEvent():


void QMessageBox::showEvent(QShowEvent *e)
{
Q_D(QMessageBox);
if (d->autoAddOkButton) {
addButton(Ok);
#if defined(Q_WS_WINCE)
d->hideSpecial();
#endif
}
if (d->detailsButton)
addButton(d->detailsButton, QMessageBox::ActionRole);
d->detectEscapeButton();
d->updateSize();

#ifndef QT_NO_ACCESSIBILITY
QAccessible::updateAccessibility(this, 0, QAccessible::Alert);
#endif
#ifdef Q_WS_WIN
HMENU systemMenu = GetSystemMenu((HWND)winId(), FALSE);
if (!d->detectedEscapeButton) {
EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED);
}
else {
EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_ENABLED);
}
#endif
QDialog::showEvent(e);
}

EDIT:
And here is also code which is graying out close button on windows when there is no detected escaped button which is the case when adding detailed text...