22

Quite often I find that I need to show and bring a window to a front. For example when the user attempts to reload the same document I simply bring up the old one. To do this I have code like this:

widget->raise();
widget->activateWindow();
widget->showNormal();

It's starting to feel like I'm missing a shortcut function. Surely this type of behaviour is quite common. Is there some preferred function that will do all of the above and/or just do the right thing on each target OS?


Note: I've just noticed a defect, thus a special function is even more important now. If the window is minimized, activateWindow does not work. This happens even if you reorder the above to showNormal first.

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
  • Yes, I've been through the docs a lot. Each OS does something slightly different, thus I need several calls. My app is the current top-level window in this case, so it usually works. – edA-qa mort-ora-y Oct 19 '11 at 11:30
  • 1
    Sounds like a bug it works fine for me in PySide. Hard to evaluate without a SSCCE that reproduces the problem. – eric Sep 12 '15 at 17:00
  • Indeed it seems like a bug, `activateWindow()` alone works for me as well. Can you test this with a newer Qt version (say, 5.6 or 5.7)? – Romário Oct 15 '16 at 04:41

2 Answers2

17

this is a working "shortcut" :

widget->setWindowState(Qt::WindowActive) ;

You can couple it with the last Qt::WindowState of the window. This notation is not very explicit though.

azf
  • 2,179
  • 16
  • 22
  • Unfortunately **raise()**, **showNormal()** - they both didn't work for me. But **setWindowState(Qt::WindowActive)** works fine on my case. Thanks... – Dewsworld Mar 30 '12 at 15:23
  • 1
    Weird... this doesn't work for me, but `activateWindow()` does. Maybe this has been fixed in Qt 5.7? – Romário Oct 15 '16 at 04:40
-3

No need for such complication. This is enough :

widget->raise();
widget->show();

By the way, you can call show() once, and unless you hide() it, there are no needs to call show() again.

BЈовић
  • 62,405
  • 41
  • 173
  • 273