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