5

I have an application where I want to ask the user a question in a QMessageBox and then respond accordingly. The problem is that on a Mac I want the dialog to show up as a Sheet, but using the open() method returns immediately.

QMessageBox* msgBox = new QMessageBox(
    QMessageBox::Question,
    "Delete Record?",
    "Are you sure you want to delete this record?"
    QMessageBox::Yes | QMessageBox::No,
    this,
    Qt::Sheet);

int ret = msgBox->exec(); // does not show up as a sheet on Mac
msgBox->open(); // shows up as a sheet but returns immediately

Is there anyway I can get which button the user pressed on a sheet without having to implement my own QDialog? Is there any signal from msgBox I connect?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Addy
  • 2,414
  • 1
  • 23
  • 43

2 Answers2

5

The document recommends using setWindowModality() instead. The following code works for me:

auto m = new QMessageBox(&window);
m->setText("some text here");
m->setWindowModality(Qt::WindowModal);
m->exec();
Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
  • But exec() would make it "application modal", that is, no other windows of the app could receive events. If you really want to receive events in other windows, then since a QMessageBox is a QDialog, you could connect to signals of QDialog and then call open(). – bootchk Oct 15 '13 at 14:45
0

This is PyQt sample from my application, but you'll get the idea:

reply = QtGui.QMessageBox.question(self, 'Delete',
            "Are you sure?", QtGui.QMessageBox.Yes |
                QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    if reply == QtGui.QMessageBox.Yes:
         #some action if YES clicked
ivica
  • 1,388
  • 1
  • 22
  • 38
  • 1
    Thank you for the response but this doesn't answer my question. Using the static method question() shows a modal dialog on Mac (as it does on Windows) and I want to show a Sheet. – Addy Sep 08 '12 at 15:26