10

I need to show simple error message

QMessageBox::critical(  nullptr, "My Title", "Text");

But the dialog appears with empty title.

What I do wrong?

PS: the OS: MacOS 10.9.1

kaa
  • 1,265
  • 5
  • 22
  • 39
  • Can you post a screenshot of the dialog? Could it be that the dialog is simply to small to show the title (On Win7, I only see "My...", the remainder is hidden by the titlebar buttons)? Can you try with a longer message text so that the dialog gets bigger? – Andreas Fester Feb 20 '14 at 10:46

3 Answers3

13

You are doing nothing wrong. From QMessageBox::setWindowTitle documentation:

Sets the title of the message box to title. On Mac OS X, the window title is ignored (as required by the Mac OS X Guidelines).

Rud Limaverde
  • 625
  • 1
  • 9
  • 14
  • If this title is ignored, I suppose that application title should be shown instead, shoudn't it? – Pavel Strakhov Feb 20 '14 at 12:28
  • No, according to the [Mac OS Guidelines](https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html) (in the **Alert** section) the alert dialog shall have no title. – Rud Limaverde Feb 20 '14 at 12:53
10

It's true that the OS X Human Interface Guidelines say that an alert dialog should have no title. However, that's not the entire story.

For one thing, there's the NSAlert::alertWithMessageText function, which still supports showing a title.

Also, on Apple's own HI Guidelines page, under the section "About Windows," it says that an About window "Has a title bar with no title." However, right above that statement, the example picture of an About box from the Finder very clearly has a title which reads, "About Finder," and in fact, the About box actually does have a title when you click "About Finder."

So Apple's own guidelines in this regard are contradictory.

Therefore, it seems to me that there's nothing really so bad about showing a window title in an alert dialog. (There's also nothing in the guidelines that says you can't create an arbitrary modal window which happens to have a title, a custom icon, a bit of static text, and an OK button.)

To paraphrase Captain Barbossa, like the Pirate Code, the Human Interface Guidelines are just that; they're more "guidelines" than actual rules.

Anyway, here's how you can get a title to show up in your QMessageBox on Mac:

QMessageBox msgBox("", "Text", QMessageBox::Critical, 0, 0, 0, nullptr, Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
msgBox.QDialog::setWindowTitle("My Title");
msgBox.exec();

I also call msgBox.setAttribute(Qt::WA_MacFrameworkScaled) in my app, which is using a slightly older version of Qt which does not want to support high-res Retina displays in QMessageBox by default.

On the other hand, there are circumstances on Mac where a sheet modal dialog (with no title bar) is more appropriate, and Qt supports this by calling QWidget::setWindowModality(Qt::WindowModal), and obviously a non-null parent window is required.

Dan Korn
  • 1,274
  • 9
  • 14
2

This works for me in Qt-5.12.1 and OSX-10.14.4:

QMessageBox msgBox("", "Text", QMessageBox::Critical, 0, 0, 0, this, Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
static_cast<QWidget*>(&msgBox)->setWindowTitle(tr("Window title"));
mechnicov
  • 12,025
  • 4
  • 33
  • 56