Show window in Qt without stealing focus
Asked Answered
T

3

48

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?

Tenner answered 8/6, 2009 at 19:59 Comment(3)
I've tried a couple of things, setting no focus, and setting disabled, but the focus was always stolen. Workaround: Open all your windows at startup and just update them during runtime.Alded
Which platform and window system?Armure
You may have better luck asking this question on <lists.trolltech.com>Introspect
T
78

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

Tenner answered 20/6, 2009 at 17:54 Comment(7)
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.Delwyn
+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!Benzofuran
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.Kind
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); !Cosgrave
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.Cosgrave
Sadly, this method seems to interfere with QWidget's restoreGeometry somehow, at least while using Qt4.8 on Windows.Addiel
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.Addiel
A
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.

Antagonistic answered 29/6, 2014 at 12:34 Comment(0)
P
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.

Pericarditis answered 18/6, 2009 at 23:18 Comment(1)
- 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.Tenner

© 2022 - 2024 — McMap. All rights reserved.