From 5279158585920b8558bf9f04861e390e174be427 Mon Sep 17 00:00:00 2001 From: re2zero Date: Thu, 31 Oct 2024 09:32:10 +0800 Subject: [PATCH] examples: add mouse events handle for Qt example Resize the widget to after client init success; and the mouse events handle. Log: add mouse events handle for Qt example. --- examples/client/qt5client.cpp | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/examples/client/qt5client.cpp b/examples/client/qt5client.cpp index 6dfaf4f9..99c5637b 100644 --- a/examples/client/qt5client.cpp +++ b/examples/client/qt5client.cpp @@ -53,8 +53,13 @@ class VncViewer : public QWidget int serverPort; std::thread *vncThread() const; void paintEvent(QPaintEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void closeEvent(QCloseEvent *event) override; private: + bool m_startFlag = false; QImage m_image; rfbClient *cl; std::thread *m_vncThread; @@ -74,6 +79,7 @@ void VncViewer::finishedFramebufferUpdate(rfbClient *cl) update(); } + void VncViewer::paintEvent(QPaintEvent *event) { event->accept(); @@ -82,6 +88,45 @@ void VncViewer::paintEvent(QPaintEvent *event) painter.drawImage(this->rect(), m_image); } +void VncViewer::mouseMoveEvent(QMouseEvent *event) +{ + if (m_startFlag) { + SendPointerEvent(cl, + event->localPos().x() / width() * cl->width, + event->localPos().y() / height() * cl->height, + (event->buttons() & Qt::LeftButton) ? 1 : 0); + } +} + +void VncViewer::mousePressEvent(QMouseEvent *event) +{ + if (m_startFlag) { + SendPointerEvent(cl, + event->localPos().x() / width() * cl->width, + event->localPos().y() / height() * cl->height, + 1); + } +} + +void VncViewer::mouseReleaseEvent(QMouseEvent *event) +{ + if (m_startFlag) { + SendPointerEvent(cl, + event->localPos().x() / width() * cl->width, + event->localPos().y() / height() * cl->height, + 0); + } +} + +void VncViewer::closeEvent(QCloseEvent *event) +{ + m_startFlag = false; + if (m_vncThread->joinable()) { + m_vncThread->join(); + } + QWidget::closeEvent(event); +} + void VncViewer::start() { cl = rfbGetClient(8, 3, 4); @@ -109,9 +154,13 @@ void VncViewer::start() std::cout << "[INFO] disconnected" << std::endl; return; } + m_startFlag = true; + + std::cout << "[INFO] screen size: " << cl->width << " x " << cl->height << std::endl; + this->resize(cl->width, cl->height); m_vncThread = new std::thread([this]() { - while (true) { + while (m_startFlag) { int i = WaitForMessage(cl, 500); if (i < 0) { std::cout << "[INFO] disconnected" << std::endl;