- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想知道我的 QMainWindow
当前是否可见并且没有被另一个应用程序的另一个窗口重叠。
我需要为 Windows、Linux 和 Mac 实现这一目标。
最佳答案
我不久前写了一个小型库,用于在 Windows、Mac OS X 和 Linux 上读取前景或最顶层的窗口信息,主要是窗口标题。你可以在这里找到源代码: https://github.com/pcmantinker/Qt-Window-Title-Reader
我在 Windows 上使用原生 Windows API,在 Linux 上使用 X11 库,在 Mac OS X 上使用 Cocoa。
下面是一个关于如何使用 Objective-C++ 在 Mac OS X 中获取事件窗口的小示例:
Mac.h
/*
Mac/Cocoa specific code for obtaining information about the frontmost window
*/
#ifndef MAC_H
#define MAC_H
#include <QtCore>
#include "windowinfo.h"
class Mac {
public:
Mac();
QList<WindowInfo> getActiveWindows();
};
#endif // MAC_H
Mac.mm
/*
Mac/Cocoa specific code for obtaining information about the frontmost window
*/
#include "mac.h"
#include "Cocoa/Cocoa.h"
Mac::Mac()
{
}
QList<WindowInfo> Mac::getActiveWindows()
{
QList<WindowInfo> windowTitles;
// get frontmost process for currently active application
ProcessSerialNumber psn = { 0L, 0L };
OSStatus err = GetFrontProcess(&psn);
CFStringRef processName = NULL;
err = CopyProcessName(&psn, &processName);
NSString *pname = (NSString *)processName;
// loop through all application windows
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSMutableDictionary* entry in (NSArray*)windowList)
{
NSString* ownerName = [entry objectForKey:(id)kCGWindowOwnerName];
NSString *name = [entry objectForKey:@"kCGWindowName" ];
NSInteger ownerPID = [[entry objectForKey:(id)kCGWindowOwnerPID] integerValue];
NSInteger layer = [[entry objectForKey:@"kCGWindowLayer"] integerValue];
if(layer == 0)
{
if([ownerName isEqualToString:pname])
{
NSRange range;
range.location = 0;
range.length = [ownerName length];
unichar *chars = new unichar[range.length];
[ownerName getCharacters:chars range:range];
QString owner = QString::fromUtf16(chars, range.length);
range.length = [name length];
chars = new unichar[range.length];
[name getCharacters:chars range:range];
QString windowTitle = QString::fromUtf16(chars, range.length);
delete[] chars;
long pid = (long)ownerPID;
WindowInfo wi;
wi.setProcessName(owner);
wi.setWindowTitle(windowTitle);
wi.setPID(pid);
windowTitles.append(wi);
}
}
}
CFRelease(windowList);
CFRelease(processName);
return windowTitles;
}
请注意,在 Cocoa 中,没有直接的方法只获取最顶层的窗口。您会获得一组窗口并循环浏览它们以找到您想要的窗口。
以下是 Windows API 的代码:
win.h
/*
Windows API specific code for obtaining information about the frontmost window
*/
#ifndef WIN_H
#define WIN_H
#include <QtCore>
#include "qt_windows.h"
#include "psapi.h"
#include "windowinfo.h"
class win : public QObject
{
Q_OBJECT
public:
win();
QList<WindowInfo> getActiveWindows();
private:
TCHAR buf[255];
};
#endif // WIN_H
win.cpp
#include "win.h"
win::win()
{
}
QList<WindowInfo> win::getActiveWindows()
{
QList<WindowInfo> windowTitles;
HWND foregroundWindow = GetForegroundWindow();
DWORD* processID = new DWORD;
GetWindowText(foregroundWindow, buf, 255);
GetWindowThreadProcessId(foregroundWindow, processID);
DWORD p = *processID;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, p);
TCHAR szProcessName[MAX_PATH];
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
CloseHandle(hProcess);
long pid = (long)p;
QString windowTitle, processName;
#ifdef UNICODE
windowTitle = QString::fromUtf16((ushort*)buf);
processName = QString::fromUtf16((ushort*)szProcessName);
#else
windowTitle = QString::fromLocal8Bit(buf);
processName = QString::fromLocal8Bit(szProcessName);
#endif
WindowInfo wi;
wi.setPID(pid);
wi.setWindowTitle(windowTitle);
wi.setProcessName(processName);
windowTitles.append(wi);
return windowTitles;
}
这是 Linux/X11 的代码:
linux_x11.h
/*
Linux/X11 specific code for obtaining information about the frontmost window
*/
#ifndef LINUX_X11_H
#define LINUX_X11_H
#include <QtCore>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include "windowinfo.h"
class linux_x11
{
public:
linux_x11();
QList<WindowInfo> getActiveWindows();
private:
Window* active(Display *disp, unsigned long *len);
char *name (Display *disp, Window win);
int *pid(Display *disp, Window win);
QString processName(long pid);
};
#endif // LINUX_X11_H
linux_x11.cpp
/*
Linux/X11 specific code for obtaining information about the frontmost window
*/
#include "linux_x11.h"
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
linux_x11::linux_x11()
{
}
/**
* Returns the window name for a specific window on a display
***/
char *linux_x11::name (Display *disp, Window win) {
Atom prop = XInternAtom(disp,"WM_NAME",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType, &type,&form,&len,&remain,&list) != Success)
return NULL;
return (char*)list;
}
/**
* Returns the pid for a specific window on a display
***/
int* linux_x11::pid(Display *disp, Window win) {
Atom prop = XInternAtom(disp,"_NET_WM_PID",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType, &type,&form,&len,&remain,&list) != Success)
return NULL;
return (int*)list;
}
/**
* Returns the active window on a specific display
***/
Window * linux_x11::active (Display *disp, unsigned long *len) {
Atom prop = XInternAtom(disp,"_NET_ACTIVE_WINDOW",False), type;
int form;
unsigned long remain;
unsigned char *list;
if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW, &type,&form,len,&remain,&list) != Success)
return NULL;
return (Window*)list;
}
/**
* Returns process name from pid (processes output from /proc/<pid>/status)
***/
QString linux_x11::processName(long pid)
{
// construct command string
QString command = "cat /proc/" + QString("%1").arg(pid) + "/status";
// capture output in a FILE pointer returned from popen
FILE * output = popen(command.toStdString().c_str(), "r");
// initialize a buffer for storing the first line of the output
char buffer[1024];
// put the contents of the buffer into a QString
QString line = QString::fromUtf8(fgets(buffer, sizeof(buffer), output));
// close the process pipe
pclose(output);
// take right substring of line returned to get process name
return line.right(line.length() - 6).replace("\n", "");
}
QList<WindowInfo> linux_x11::getActiveWindows()
{
QList<WindowInfo> windowTitles;
unsigned long len;
Display *disp = XOpenDisplay(NULL);
Window *list;
char *n;
int* p;
list = (Window*)active(disp,&len);
if((int)len > 0)
{
for (int i=0;i<(int)len;i++) {
n = name(disp,list[i]);
p = pid(disp, list[i]);
long p_id = 0;
QString pName;
QString windowTitle;
if(p!=NULL)
{
p_id = *p; // dereference pointer for obtaining pid
pName = processName(p_id);
}
if(n!=NULL)
windowTitle = QString::fromUtf8(n);
WindowInfo wi;
wi.setWindowTitle(windowTitle);
wi.setProcessName(pName);
wi.setPID(p_id);
windowTitles.append(wi);
delete n;
delete p;
}
}
delete list;
XCloseDisplay (disp);
return windowTitles;
}
X11 代码可能会变得非常丑陋且难以理解,但这应该能让您入门。自从我直接处理 X11 以来已经有一段时间了,所以我现在不能准确地告诉您每个辅助方法的作用。
我对代码进行了抽象,使得每个特定于平台的代码片段都具有相同的方法签名。然后我检查它是否在 Mac OS X、Windows 或 Linux 上编译并实例化正确的类。以下是所有内容的关联方式:
#include "windowtitlereader.h"
WindowTitleReader::WindowTitleReader()
{
qDebug() << "WindowTitleReader::WindowTitleReader()";
// refresh window reading every 10ms
timer = new QTimer(this);
timer->setInterval(10);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(getWindowTitle()));
}
WindowTitleReader::~WindowTitleReader()
{
delete timer;
delete m_pid;
delete m_processName;
}
void WindowTitleReader::getWindowTitle()
{
qDebug() << "WindowTitleReader::getWindowTitle()";
#ifdef Q_WS_WIN
win w;
m_activeWindows = w.getActiveWindows();
#endif
#ifdef Q_WS_MACX
Mac m;
m_activeWindows = m.getActiveWindows();
#endif
#ifdef Q_WS_X11
linux_x11 l;
m_activeWindows = l.getActiveWindows();
#endif
for(int i = 0; i < m_activeWindows.count(); i++)
qDebug() << "PID: " << m_activeWindows[i].getPID() << " Process Name: " << m_activeWindows[i].getProcessName() << " Window Title: " << m_activeWindows[i].getWindowTitle();
}
如果需要,您可以将刷新率更改为较慢的刷新率,但我每 10 毫秒运行一次更新,以便在窗口聚焦上获得近乎实时的更新。
我编写这个库主要是为了读取网络浏览器和视频游戏的窗口标题,这样我就可以估计人们在计算机上玩某些游戏的时间。我将此作为我正在构建的游戏指标应用程序的一部分进行编写。
关于c++ - 检查我的 QMainWindow 当前在 Qt 中是否可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18183190/
https://stackoverflow.com/a/13661255/462608 QMainWindow comes with its own layout, you can't set tha
我使用 QMainWindow 作为我的主 QMainWindow 的 child。这样我就得到了另一个区域,我可以将其用于可停靠小部件 (QDockWidget)。 根据以下帖子,这没问题,它也非常
重点关注这部分代码: prect = self.parent.rect() # Show Window2),Window2 不相对于父窗口居中显示。 当我使用 prect1.center() 时,它
这个相当少的代码创建了一个带有三个右键单击选项的系统托盘项。一个是QDialog的实例,另一个是QMainWindow,还有Quit。它适用于一个系统托盘驱动的应用程序,它将有一些 qdialogs
我想将 QMainWindow 实例放在另一个 QWidget 中(例如另一个 QMainWindow 的 centralWidget)。 我想知道为什么它不起作用? QMainWindow 直接继承
我正在 OSX 下使用 Qt Creator (Qt5.10) 编写 GUI。我有一个用于主应用程序窗口的 QMainWindow。在这里面我有一个 QTab 小部件,其中我有整个小部件(我们称它为
嗯,主题。 我在桌面 Qt4 应用程序中使用停靠栏小部件,但我不喜欢停靠栏形式的布局。我希望右侧 停靠栏占据右上 角,默认情况下由顶部 停靠栏占据。 我用谷歌搜索(认真地!)我能用这个做什么,我找到了
我是 PySide 的新手。我遇到一个问题:当我从另一个 QmainWIndow(主 UI)触发 QAction 时,一个新的 QMainWindow 闪烁并消失了。我在这里发布示例代码来说明以上内容
我的问题: 我想自定义标题栏 的工作方式和查找我的应用程序的方式。 我的想法: 我在 Qt Designer 中创建了一个新的 QWidget 表单,并向其中添加了一个 QWidget。我在构造函数中
我想在屏幕中间显示我的项目主窗口。当我调用“self.show()”时,窗口会显示在屏幕中间。 最佳答案 我知道你已经解决了,但我为那些有同样问题的人做这个回答。我发布它主要是因为你要求 pyQt 而
为什么 QMainWindow 的析构函数不是虚拟的?如果我从 QMainWindow 继承了一个类 MainWindow 并在它上面有一些指针属性,我就不能使用 MainWindow 的析构函数删除
我正在尝试重建一个屏幕记录PyQt应用程序,ScreenToGIF对我来说是一个非常好的演示,它创建了一个只有边框并在“Central Widgets”中记录内容的界面,如下所示: 主要功能为: 边框
我对 Qt 比较陌生。 我正在编写一个应用程序,我在其中生成伪随机数和 key ,我想用 QMainWindow 中捕获的随机鼠标移动来为生成器播种。此外,我希望能够在用户按下 QButton 时关闭
如何将第一个参数传递给 QMainWindow? 我希望能够将多个不同的参数传递给 QMainWindow。我的问题是,即使在我修改了构造函数以允许传递第二个/第三个/第四个参数之后,我仍然找不到实例
我尝试创建类似窗口池的东西。您可以在程序中的任何地方使用这些窗口来显示图形和绘图等。小部件运行良好,但目前的主要问题是创建池的尝试受挫。一个非 QObject-Object 应该表示一个 QMainW
我制作了一个快速测试程序,执行时似乎没有显示任何内容。这是我的代码: z_lock.h #ifndef Z_LOCK_H #define Z_LOCK_H #include class z_lock
是否可以制作一个未修饰的 QMainWindow,如果可以,我该怎么做? 最佳答案 是的, mainWin->setWindowFlags(Qt::CustomizeWindowHint); 参见 Q
我正在尝试编译 cutecash(一个 qt 项目)并且遇到了一些麻烦。我只是在做标准: cmake . make 但是,我遇到无法找到 QMainWindow 和 QString 等错误。这是错误:
一个最小的例子: class MainWindow(QtGui.QMainWindow): def __init__(self, parent = None): QtGui.QMain
我在 PyQT5 中遇到错误的布局。我究竟做错了什么?是否有一些预定义的小字段大小或类似的?我将主窗口创建为 QMainWindow,并在其中创建一个小部件作为中央小部件。它是这样的: class M
我是一名优秀的程序员,十分优秀!