Getting the window title of a non Qt application
-
wrote on 19 Jul 2011, 23:20 last edited by
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.
-
wrote on 20 Jul 2011, 00:20 last edited by
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...
-
wrote on 20 Jul 2011, 07:04 last edited by
@QApplication::activeWindow()->windowTitle()@
-
wrote on 20 Jul 2011, 08:55 last edited by
[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? -
wrote on 20 Jul 2011, 10:07 last edited by
[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. -
wrote on 21 Jul 2011, 03:01 last edited by
[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.
-
wrote on 21 Jul 2011, 05:18 last edited by
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.
-
wrote on 27 Aug 2011, 20:57 last edited by
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. -
wrote on 13 Aug 2012, 07:01 last edited by
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.