让我们看看下面的测试应用程序: 的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
正如你看到的,有2个移动事件,应用程序最初创建时,一个,当我完成窗口的动作。我正在用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协议。
您应该也可以考虑用户使用键盘移动窗口的情况。 (至少在Windows中这是可能的,我不知道X11。) – 2012-10-18 14:55:50
QMoveEvent每次移动时都会发布,无论移动的方式如何。但是,有QResizeEvent,这也可能影响窗口中的动画。 – divanov