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. Getting the window title of a non Qt application
QtWS25 Last Chance

Getting the window title of a non Qt application

Scheduled Pinned Locked Moved General and Desktop
9 Posts 6 Posters 13.4k 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
    Shuukun
    wrote on 19 Jul 2011, 23:20 last edited by
    #1

    Hi, I want to know if it's possible to get window titles or application/process names, from windows focused outside of a Qt application. For example, if i switch from my Qt application to Firefox, I'd like to print out the title of the window i lost focus to. In this case "Firefox" or "Firefox.exe" would be printed out to the console. Thanks.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      LinusA
      wrote on 20 Jul 2011, 00:20 last edited by
      #2

      Even though my answer is not related to Qt, you can achieve this on Windows using the WinAPI. First get the currently "focused" window with GetForegroundWindow() http://msdn.microsoft.com/en-us/library/ms633505(v=vs.85).aspx . Then retrieve the title caption with GetWindowText() http://msdn.microsoft.com/en-us/library/ms633520(v=vs.85).aspx . However, you need the basics of Win-API-Programming, have to include the Windows headers, and are platform dependant. Also, you might run into subtle problems (don't know, but just saw this link: http://blogs.msdn.com/b/oldnewthing/archive/2003/08/21/54675.aspx )

      If there was a Qt-specific (platform-independent) way, that would be interesting to know...

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on 20 Jul 2011, 07:04 last edited by
        #3

        @QApplication::activeWindow()->windowTitle()@

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on 20 Jul 2011, 08:55 last edited by
          #4

          [quote author="cincirin" date="1311145479"]@QApplication::activeWindow()->windowTitle()@[/quote]

          That does not make sense. It works only for the application windows.
          You simply need to use platform specific code to know the focused window name. Which OSes are you interested in?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on 20 Jul 2011, 10:07 last edited by
            #5

            [quote author="peppe" date="1311152126"]That does not make sense. It works only for the application windows. [/quote]
            You're right. I was too rushed.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Shuukun
              wrote on 21 Jul 2011, 03:01 last edited by
              #6

              [quote author="peppe" date="1311152126"]
              [quote author="cincirin" date="1311145479"]@QApplication::activeWindow()->windowTitle()@[/quote]

              That does not make sense. It works only for the application windows.
              You simply need to use platform specific code to know the focused window name. Which OSes are you interested in?[/quote]

              Both Windows and Mac. I'm sure i can find a way in Windows using the WinAPI as LinusA suggested. The problem is finding a way on a Mac.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 21 Jul 2011, 05:18 last edited by
                #7

                Since you need platform specific code, I think you might have more luck for an answer in a mac development specific help channel. Qt is just C++, so not much special about the fact that you use it.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Shuukun
                  wrote on 27 Aug 2011, 20:57 last edited by
                  #8

                  Research and findings led me to these solutions.

                  For Windows:
                  @#include "qt_windows.h"
                  TCHAR buf[255]
                  GetWindowText(GetForegroundWindow(), buf, 255);
                  qDebug() << QString::fromUtf16(buf);@

                  For Macs:
                  In your .pro file put LIBS += -framework Cocoa
                  I also noticed that including or importing Cocoa/Cocoa.h does not work in a header file. It generates over 9000 errors for me. It works perfectly in a .mm source file though.
                  @#import "Cocoa/Cocoa.h"
                  int i =0;
                  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                  NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

                      for (NSDictionary *window in windows) {
                          NSString *owner = [window objectForKey:@"kCGWindowOwnerName" ];
                          NSString *name = [window objectForKey:@"kCGWindowName" ];
                          NSNumber *layer = [window objectForKey:@"kCGWindowLayer"];
                  
                              if([layer intValue] == 0 && i == 0){/* only returns the first window title. 
                  

                  Window titles are returned are in order from front to back and i only want the
                  frontmost active window title.*/
                  NSString *title = [owner stringByAppendingString:@" - "];
                  title = [title stringByAppendingString:name];
                  QString foregroundAppTitle = qt_mac_NSStringToQString(title);
                  qDebug() << foregroundAppTitle;
                  i++;
                  }
                  }@
                  I chose this method of getting the frontmost window title because i didn't want to run an external process using QProcess and applescript. The qt_mac_NSStringToQString function was found "here":http://www.qtcentre.org/threads/34752-NSString-to-QString.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maxvanceffer
                    wrote on 13 Aug 2012, 07:01 last edited by
                    #9

                    Could your provide the full example of mac os version. Because mess of mm, h, cpp files in Mac OS... It's hard to understand where release this functions.

                    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