我用Qt编辑器用ct编码。我可以在屏幕上显示所有窗口,但我想直接检测它们的移动。有没有办法通过窗口发出的信号来检测运动?
答案 0 :(得分:-1)
您可以尝试从目标窗口创建QWindow,然后使用QWidget :: createWindowContainer()将其包装在QWidget中。
您可以查看此QTBUG主题:https://bugreports.qt.io/browse/QTBUG-40320
需要一些努力才能使其正常运行。捕获的窗口不保持其初始尺寸,并且释放窗口表现得很奇怪。阅读最后的QTBUG评论以找到改进。
我在此代码中添加了一个事件过滤器,以实时捕获窗口位置。但这可能不令人满意。
class EventFilter : public QObject
{
Q_OBJECT
public:
EventFilter(){}
virtual ~EventFilter(){}
protected:
bool eventFilter(QObject *obj, QEvent *event);
} ;
bool EventFilter::eventFilter(QObject *obj, QEvent *event)
{
qDebug() << event->type() ;
if (event->type() == QEvent::Move) {
QMoveEvent *moveEvent = static_cast<QMoveEvent *>(event);
qDebug() << "position" << moveEvent->pos() ;
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}
我从QTBUG代码段中删除了第3部分,并在内部窗口小部件上安装了事件处理程序。您也可以删除计时器。
// From https://bugreports.qt.io/browse/QTBUG-40320
int main(int argc, char *argv[])
{
// Windows: Find HWND by window title
WId id = (WId)FindWindow(NULL, L"Calculator");
if (!id)
return -1;
QApplication a(argc, argv);
// Optional
QTimer t;
t.start(2500);
// Part 1
QWindow* window = QWindow::fromWinId(id);
window->show();
window->requestActivate();
// Optional
QObject::connect(&t, &QTimer::timeout, [=]
{
qDebug() << "=== Inner QWindow ===";
qDebug() << "Geometry:" << window->geometry();
qDebug() << "Active?:" << window->isActive();
qDebug() << "Flags:" << window->flags();
});
// Part 2
QWidget* widget = QWidget::createWindowContainer(window);
widget->show();
// Optional
QObject::connect(&t, &QTimer::timeout, [=]
{
qDebug() << "=== Outer QWidget ===";
qDebug() << "Geometry:" << widget->geometry();
qDebug() << "Active?" << widget->isActiveWindow();
qDebug() << "Flags:" << widget->windowFlags();
});
// Realtime position
EventFilter filter ;
widget->installEventFilter( &filter ) ;
return a.exec();
}
输出:
=== Inner QWindow ===
Geometry: QRect(0,0 640x480)
Active?: true
Flags: QFlags<Qt::WindowType>(ForeignWindow)
=== Outer QWidget ===
Geometry: QRect(2489,29 640x480)
Active? true
Flags: QFlags<Qt::WindowType>(Window|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint)
QEvent::Type(Move)
position QPoint(2484,29)
QEvent::Type(Move)
position QPoint(2481,30)
QEvent::Type(Move)
position QPoint(2478,31)
QEvent::Type(Move)
position QPoint(2474,31)
另见http://blog.qt.io/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/