Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using AlwaysActivateWindow to gain foreground in WIN10 using QT6.2
Forum Updated to NodeBB v4.3 + New Features

Using AlwaysActivateWindow to gain foreground in WIN10 using QT6.2

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 869 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    supa5000
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      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 file

      QT += 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);
      }
      
      1 Reply Last reply
      1
      • S Offline
        S Offline
        supa5000
        wrote on last edited by
        #3

        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 {
              |                ^
        
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved