Skip to content

Commit

Permalink
examples: add mouse events handle for Qt example
Browse files Browse the repository at this point in the history
Resize the widget to after client init success; and the mouse events handle.

Log: add mouse events handle for Qt example.
  • Loading branch information
re2zero committed Oct 31, 2024
1 parent f7735c4 commit 42da870
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion examples/client/qt5client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -74,6 +79,7 @@ void VncViewer::finishedFramebufferUpdate(rfbClient *cl)

update();
}

void VncViewer::paintEvent(QPaintEvent *event)
{
event->accept();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 42da870

Please sign in to comment.