47

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?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
dutchmega
  • 1,384
  • 2
  • 12
  • 16

3 Answers3

76

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

pajton
  • 15,828
  • 8
  • 54
  • 65
dutchmega
  • 1,384
  • 2
  • 12
  • 16
  • 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. – njahnke Oct 16 '11 at 00:00
  • +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! – Cameron Tinker Mar 03 '12 at 00:31
  • 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. – J.Churchill Nov 16 '12 at 16:05
  • 3
    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 Aug 25 '13 at 23:58
  • 1
    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. – gremwell Aug 26 '13 at 00:52
  • Sadly, this method seems to interfere with `QWidget`'s `restoreGeometry` somehow, at least while using Qt4.8 on Windows. – Predelnik Sep 01 '15 at 12:22
  • 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. – Predelnik Sep 01 '15 at 12:35
11

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.

Yash
  • 6,644
  • 4
  • 36
  • 26
4

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.

Troubadour
  • 13,334
  • 2
  • 38
  • 57
  • - 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. – dutchmega Jun 20 '09 at 17:32