10

I have created a program that runs alongside an application in fullscreen. I would like the QMessageBox from my program to be displayed on top of the application that runs in fullscreen.

The platform is Windows 7 and i am using Qt.

I have tried:

QMessageBox *msgBox = new QMessageBox;
msgBox->setParent(0);
msgBox->setWindowTitle(title);
msgBox->setText(text);
msgBox->setWindowFlags(Qt::WindowStaysOnTopHint);
msgBox->show();

With no luck. Any hints?

Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
  • You need to use exec(), so call: `msgBox->exec();` instead. – vahancho May 07 '14 at 08:47
  • Thank you, but it did not work unfortunately. The window stays behind the currently active window. – Nicolai Lissau May 07 '14 at 08:49
  • Than, set the massage box's parent to be that currently active window. – vahancho May 07 '14 at 08:50
  • The application running in fullscreen is not mine. Can I set the parent to the active window with Qt, or do i have to use the winapi? – Nicolai Lissau May 07 '14 at 09:00
  • because i tried: msgBox->setParent(QApplication::activeWindow()); but without effect – Nicolai Lissau May 07 '14 at 09:02
  • 1
    Yes, you can. Instead of null in `msgBox->setParent(0);` you need to set the pointer to the active window. Can you get it? What is that window? Please provide more details. – vahancho May 07 '14 at 09:02
  • Does a static messagebox method also fail to be topmost? QMessageBox::warning(NULL, "Title", "MessageText"); – TheDarkKnight May 07 '14 at 09:02
  • @Merlin: The static messagebox also fail to be topmost. I just found however, that if i do the steps that produces the error (and thereby displays the messagebox) twice, then the messagebox does show on top. – Nicolai Lissau May 07 '14 at 09:11
  • @Vahacho I just found that QApplication::activeWindow() returns a null pointer. The fullscreen program is the main program. An interface where the user is working. My program is a program that runs alongside to check if the files are available in the working folder. Is this enough detail? – Nicolai Lissau May 07 '14 at 09:12
  • Try msgBox->activateWindow() – TheDarkKnight May 07 '14 at 09:13
  • Interestingly, reading the docs for activateWindow(), it states "On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window...This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application." This sounds like you can't achieve what you're asking. – TheDarkKnight May 07 '14 at 09:15
  • I could not make ´activateWindow()´ work either so that makes sense, and it does agree with the answer that @Sebastian Lange gave. Hmm... – Nicolai Lissau May 07 '14 at 09:16
  • @Attaque, well, try to use `QApplication::topLevelWidgets()` before showing the message box to see whether your full screen windows is listed among top level windows. If yes, use it as a message box parent. – vahancho May 07 '14 at 09:20
  • Okay so i forgot that i never show my main window. In main.cpp i call MainWindow w; but do not show it. If i show the main dialog (w.show;) and then hide it again, the error dialog will be on top with just the code that I originally posted. So i guess the context for the error dialog is not created or some sort? But i thought that calling the dialog with 0 as parent would create it's own context? – Nicolai Lissau May 07 '14 at 09:31

1 Answers1

3

Try msgBox->raise(); will notify the user in taskbar, using setWindowFlags(Qt::WindowStaysOnTopHint); you eventually could make it stay on top (evtl. minimize/restore). But a windowmanager, not depending on os, by design should not allow any application to just "steal" the focus from another application, therefor the user still needs to activate (click) your window for gaining focus.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38