Qt 5.10 Book Examples
MouseObserver.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MouseObserver.h --
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include "MouseObserver.h"
12 
13 // ----------------------------------------------------------------------
14 MouseObserver::MouseObserver(QWidget* pwgt /*= 0*/) : QLabel(pwgt)
15 {
16  setAlignment(Qt::AlignCenter);
17  setText("Mouse interactions\n(Press a mouse button)");
18 }
19 
20 // ----------------------------------------------------------------------
21 /*virtual*/void MouseObserver::mousePressEvent(QMouseEvent* pe)
22 {
23  dumpEvent(pe, "Mouse Pressed");
24 }
25 
26 // ----------------------------------------------------------------------
27 /*virtual*/void MouseObserver::mouseReleaseEvent(QMouseEvent* pe)
28 {
29  dumpEvent(pe, "Mouse Released");
30 }
31 // ----------------------------------------------------------------------
32 /*virtual*/ void MouseObserver::mouseMoveEvent(QMouseEvent* pe)
33 {
34  dumpEvent(pe, "Mouse Is Moving");
35 }
36 
37 // ----------------------------------------------------------------------
38 void MouseObserver::dumpEvent(QMouseEvent* pe, const QString& strMsg)
39 {
40  setText(strMsg
41  + "\n buttons()=" + buttonsInfo(pe)
42  + "\n x()=" + QString::number(pe->x())
43  + "\n y()=" + QString::number(pe->y())
44  + "\n globalX()=" + QString::number(pe->globalX())
45  + "\n globalY()=" + QString::number(pe->globalY())
46  + "\n modifiers()=" + modifiersInfo(pe)
47  );
48 }
49 
50 // ----------------------------------------------------------------------
51 QString MouseObserver::modifiersInfo(QMouseEvent* pe)
52 {
53  QString strModifiers;
54 
55  if(pe->modifiers() & Qt::ShiftModifier) {
56  strModifiers += "Shift ";
57  }
58  if(pe->modifiers() & Qt::ControlModifier) {
59  strModifiers += "Control ";
60  }
61  if(pe->modifiers() & Qt::AltModifier) {
62  strModifiers += "Alt";
63  }
64  return strModifiers;
65 }
66 
67 // ----------------------------------------------------------------------
68 QString MouseObserver::buttonsInfo(QMouseEvent* pe)
69 {
70  QString strButtons;
71 
72  if(pe->buttons() & Qt::LeftButton) {
73  strButtons += "Left ";
74  }
75  if(pe->buttons() & Qt::RightButton) {
76  strButtons += "Right ";
77  }
78  if(pe->buttons() & Qt::MidButton) {
79  strButtons += "Middle";
80  }
81  return strButtons;
82 }
83 
QString modifiersInfo(QMouseEvent *pe)
void dumpEvent(QMouseEvent *pe, const QString &strMessage)
virtual void mouseReleaseEvent(QMouseEvent *pe)
QString buttonsInfo(QMouseEvent *pe)
virtual void mouseMoveEvent(QMouseEvent *pe)
MouseObserver(QWidget *pwgt=0)
virtual void mousePressEvent(QMouseEvent *pe)