Qt 5.10 Book Examples
MultiTouchWidget.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MultiTouchWidget.cpp
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 <QtGui>
12 #include "MultiTouchWidget.h"
13 
14 // ----------------------------------------------------------------------
15 MultiTouchWidget::MultiTouchWidget(QWidget* pwgt/*= 0*/) : QWidget(pwgt)
16 {
17  setAttribute(Qt::WA_AcceptTouchEvents);
18 
19  m_lstCols << Qt::cyan << Qt::green << Qt::blue << Qt::black
20  << Qt::red << Qt::magenta << Qt::darkYellow
21  << Qt::gray << Qt::darkCyan << Qt::darkBlue;
22 }
23 
24 // ----------------------------------------------------------------------
25 /*virtual*/ void MultiTouchWidget::paintEvent(QPaintEvent*)
26 {
27  QPainter painter(this);
28  painter.setRenderHint(QPainter::Antialiasing, true);
29 
30  int nColsCount = m_lstCols.count();
31  foreach (QTouchEvent::TouchPoint tp, m_lstTps) {
32  switch (tp.state()) {
33  case Qt::TouchPointStationary:
34  continue;
35  default:
36  QColor c(m_lstCols.at(tp.id() % nColsCount));
37  painter.setPen(c);
38  painter.setBrush(c);
39 
40  QRectF r1(tp.pos(), QSize(20, 20));
41  QRectF r2(tp.startPos(), QSize(20, 20));
42  painter.drawEllipse(r1.translated(-10, -10));
43  painter.drawEllipse(r2.translated(-10, -10));
44 
45  painter.drawLine(tp.pos(), tp.startPos());
46  }
47  }
48 }
49 
50 // ----------------------------------------------------------------------
51 /*virtual*/ bool MultiTouchWidget::event(QEvent* pe)
52 {
53  switch (pe->type()) {
54  case QEvent::TouchBegin:
55  case QEvent::TouchUpdate:
56  case QEvent::TouchEnd:
57  {
58  QTouchEvent* pte = static_cast<QTouchEvent*>(pe);
59  m_lstTps = pte->touchPoints();
60  update();
61  }
62  default:
63  return QWidget::event(pe);
64  }
65  return true;
66 }
virtual void paintEvent(QPaintEvent *)
MultiTouchWidget(QWidget *pwgt=0)