简体   繁体   中英

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

I'm trying to create a pseudo remote control widget, the remote control widget (an overridden QLabel) receives a screenshot (pixmap) of the widget to be controlled every 2 seconds or so through a REST api. I've overridden the QLabel mouse events and can store the positions of clicks on the overridden QLabel.

How can I convert these positions into mouse events and execute them on the "remote controlled" widget? I've attached the cpp of the overridden QLabel and would appreciate any input.

#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";
}

You can try this:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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