繁体   English   中英

如何将点击从一个 Qt Widget 转移到另一个?

[英]How can I transfer clicks in from one Qt Widget to another?

我正在尝试创建一个伪远程控制小部件,远程控制小部件(一个覆盖的 QLabel)每 2 秒左右通过 REST api 接收要控制的小部件的屏幕截图(像素图)。 我已经覆盖了 QLabel 鼠标事件,并且可以存储在覆盖的 QLabel 上的点击位置。

如何将这些位置转换为鼠标事件并在“远程控制”小部件上执行它们? 我附上了被覆盖的 QLabel 的 cpp 并希望得到任何输入。

#include "RemoteControlLabel.h"
#include <QDebug>
#include <QMouseEvent>

RemoteControlLabel::RemoteControlLabel(QWidget* parent) : QLabel(parent)
{

}

RemoteControlLabel::~RemoteControlLabel()
{

}

void RemoteControlLabel::mousePressEvent(QMouseEvent* event)
{
    QPoint pos = event->pos();
    qDebug() << "mouse pressed at " << pos;
}

void RemoteControlLabel::mouseReleaseEvent(QMouseEvent* event)
{
    qDebug() << "Mouse released";
}

你可以试试这个:

virtual void RemoteControlLabel::mousePressEvent(QMouseEvent* event) override
{
    QPoint pos = event->pos();
    qDebug() << "mouse pressed at " << pos;
    // create new event on the stack
    QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
    // use sendEvent - it sends the event directly
    QApplication::sendEvent(remotelyControlledWidget, &event);
    // at the end of scope event will be automatically deleted, which is our intention
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM