Using AlwaysActivateWindow to gain foreground in WIN10 using QT6.2
-
Dear all,
The background:
- Our application is "single instance" type of application, that during startups checks if another instance is running
- We have custom files assosiated with our application
- When user clicks on file explorer our file, the windows launches our application
- If there is another instance running, it will just send the filenames and the "old" instance opens up the file
Now on the last bullet, i would really much like to get the "old" main-window to foreground. Like i think most of windows software does.
No what i have
win->show(); win->raise(); win->requestActivate();
Only makes the window to "blink" on taskbar if the window is open, but on background.
During my googling i found out apparantly there is https://doc.qt.io/qt-5/qwindowswindowfunctions.html - but these are not available at 6?
When looking at the source code, i did find some references that the functinality would be there https://code.qt.io/cgit/qt/qtbase.git/tree/src/plugins/platforms/windows/qwindowswindow.cpp?h=6.2.2#n2625 - but how should this be used?
Br,
Pauli -
As I tested, to get the equivalent effect of qt5 version's
QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow)
in qt6, you'll first need to add this in your
.pro
fileQT += gui-private
Then in you source code
#include <private/qguiapplication_p.h> ... if(auto inf = qApp->nativeInterface<QNativeInterface::Private::QWindowsApplication>()) { inf->setWindowActivationBehavior(QNativeInterface::Private::QWindowsApplication::AlwaysActivateWindow); }
-
Thanks @Bonnie ! It worked out of the box.
Only thing i needed (for the record, if fellow googlers end up here) as i use CMake based build system was to add
include_directories(${Qt6Gui_PRIVATE_INCLUDE_DIRS})
And due compiling flags (warnings as errors):
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" #include <private/qguiapplication_p.h> #pragma GCC diagnostic pop
Again for the record, the warning - suppressed by the above - was:
In file included from C:/QT/6.3.0/mingw_64/include/QtCore/6.3.0/QtCore/private/qobject_p.h:64, from C:/QT/6.3.0/mingw_64/include/QtCore/6.3.0/QtCore/private/qcoreapplication_p.h:63, from C:/QT/6.3.0/mingw_64/include/QtGui/6.3.0/QtGui/private/qguiapplication_p.h:59, from C:/msys64/home/salmenrinne/pxr-studio/src/main/main.cpp:188: C:/QT/6.3.0/mingw_64/include/QtCore/6.3.0/QtCore/private/qproperty_p.h:263:16: error: ISO C++ prohibits anonymous structs [-Wpedantic] 263 | struct { | ^