让我们考虑以下测试应用程序:
main.cpp
#include <QApplication>
#include "win.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Win w;
w.show();
return app.exec();
}
win.h:
#include <QWidget>
#include <QEvent>
#include <QMoveEvent>
#include <QDebug>
class Win : public QWidget
{
public:
Win(QWidget *parent = 0) : QWidget(parent) {
this->installEventFilter(this);
}
protected:
bool eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::Move) {
QMoveEvent *moveEvent = static_cast<QMoveEvent*>(event);
qDebug() << "Move event:" << moveEvent->pos();
} else {
qDebug() << "Event type:" << event->type();
}
return QWidget::eventFilter(obj, event);
}
};
这个应用程序只在自身上安装事件过滤器,并使用特殊格式打印所有接收到的事件到控制台,以区分QMoveEvent。
典型日志:
Event type: 203
Event type: 75
Move event: QPoint(0,0)
Event type: 14
Event type: 17
Event type: 26
Event type: 74
Event type: 77
Move event: QPoint(66,52)
Event type: 12
Event type: 24
Event type: 99
Event type: 77
Event type: 12
Event type: 10
Event type: 11
Move event: QPoint(308,356)
Event type: 19
Event type: 25
Event type: 99
Event type: 18
Event type: 27
Event type: 77
如您所见,有两个移动事件,一个是应用程序最初创建时,另一个是在我完成窗口移动后。我使用的是Qt 4.8.1和XOrg 7.6进行测试。
检查原始X事件
- 运行测试应用程序。
- 获取测试应用程序的窗口ID。要执行此操作,请在命令行中执行
xwininfo -name WINDOW_NAME
,其中WINDOW_NAME
是测试应用程序窗口的名称。另一种选择是使用没有参数的xwininfo,然后您必须使用鼠标指针选择测试应用程序窗口。
- 运行X事件监视器
xev -id 0x2a00002
,其中0x2a00002
是在上一步中找到的窗口ID。这将打印您的窗口从X服务器接收到的X事件。ConfigureNotify
是QMoveEvent
的X协议对应项。