Show window in Qt without stealing focus

25,799

Solution 1

It took me a while to find it but I found it: setAttribute(Qt::WA_ShowWithoutActivating);

This forces the window not to activate. Even with the Qt::WindowStaysOnTopHint flag

Solution 2

If you want to make floating preview box/ any other widget just use below

thumbnail = new QLabel;
thumbnail->setAttribute(Qt::WA_ShowWithoutActivating);
thumbnail->setParent(0);
thumbnail->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

Qt::Tool is important flag to make it work. I mean not stealing focus.

Solution 3

Widgets don't accept focus by default but presumably you haven't created a plain widget? Which subclass was it? QMainWindow or something else?

It's possible the window subclasses default to accepting focus so try explicitly calling QWidget::setFocusPolicy with Qt::NoFocus before calling QWidget::show().

Also, make sure you're not calling QWidget::activateWindow() on the window or any of its widgets at any point.

Share:
25,799
dutchmega
Author by

dutchmega

Updated on July 09, 2022

Comments

  • dutchmega
    dutchmega almost 2 years

    I'm using the Qt library to show a slideshow on the second monitor when the user isn't using the second monitor. An example is the user playing a game in the first monitor and showing the slideshow in the second monitor.

    The problem is that when I open a new window in Qt, it automatically steals the focus from the previous application. Is there any way to prevent this from happening?

  • dutchmega
    dutchmega almost 15 years
    - I'm creating a plain QWidget-derived class without any flags - setFocusPolicy() doesn't seem to have any effect. - Qt::WindowStaysOnBottomHint works but the window won't be visible on open (under other applictions) Interestingly the first time the window is opened, it doesn't take the focus. Only when the window is reopened it takes the focus from other Windows-applocations.
  • njahnke
    njahnke over 12 years
    this does not appear to be effective on a QToolBar under os x 10.6 (cocoa qt 4.7.x). focus is stolen from the main window and given to the QToolBar after it has been show()n.
  • Cameron Tinker
    Cameron Tinker about 12 years
    +1 for finding this attribute. I was having issues with an app I'm developing stealing focus from other top level windows. This did the trick!
  • deathAdder
    deathAdder over 11 years
    This attribute is only available in 4.4.0 onwards... for my case (using 4.1.3 :( ) I had to save the active window with QApplication::activeWindow(), show the new window and then call activateWindow() on the originally active window to restore focus.
  • gremwell
    gremwell over 10 years
    You are my hero. I've been trying to implement my own transparent tooltip widget, and it seems that on windows the Qt::Tooltip window flag means that you can't have a transparent tooltip... Now however I can say setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_ShowWithoutActivating); setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus); !
  • gremwell
    gremwell over 10 years
    Actually, I had to use setAttribute(Qt::WA_ShowWithoutActivating); with the window flags: setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowSystemMenuHint); to get it to work.
  • Predelnik
    Predelnik over 8 years
    Sadly, this method seems to interfere with QWidget's restoreGeometry somehow, at least while using Qt4.8 on Windows.
  • Predelnik
    Predelnik over 8 years
    Since my previous comment is a bit misleading, i'd also like to add that it seems to be winapi deficency that SW_SHOWNOACTIVATE does the same thing as SW_NORMAL without activating window but there's no similar constant for showing windows either as maximized or minimzed without activating them. That's really unfortunate.