QtBase  v6.3.1
main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <QtWidgets>
52 
54 {
55 public:
56  TouchableItem() : QGraphicsRectItem(50, 50, 400, 400)
57  {
60  }
61 protected:
63  {
64  const bool ret = QGraphicsRectItem::sceneEvent(e);
65  switch (e->type()) {
66  case QEvent::TouchBegin:
68  case QEvent::TouchEnd:
69  {
70  QTouchEvent *te = static_cast<QTouchEvent *>(e);
71  for (const QEventPoint &tp : te->touchPoints()) {
72  QGraphicsEllipseItem *diameterItem = nullptr;
73  QSizeF ellipse = tp.ellipseDiameters();
74  if (ellipse.isNull()) {
75  ellipse = QSizeF(5, 5);
76  } else {
77  diameterItem = new QGraphicsEllipseItem(QRectF(tp.pos().x() - ellipse.width() / 2, tp.pos().y() - ellipse.height() / 2,
78  ellipse.width(), ellipse.height()), this);
79  diameterItem->setPen(QPen(Qt::red));
80  diameterItem->setBrush(QBrush(Qt::red));
81  if (ellipse.width() > qreal(2) && ellipse.height() > qreal(2))
82  ellipse.scale(ellipse.width() - 2, ellipse.height() - 2, Qt::IgnoreAspectRatio);
83  }
84  QGraphicsItem *parent = diameterItem ? static_cast<QGraphicsItem *>(diameterItem) : static_cast<QGraphicsItem *>(this);
85  QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(tp.pos().x() - ellipse.width() / 2,
86  tp.pos().y() - ellipse.height() / 2,
87  ellipse.width(), ellipse.height()), parent);
88  ellipseItem->setPen(QPen(Qt::blue));
89  ellipseItem->setBrush(QBrush(Qt::blue));
90  }
91  te->accept();
92  return true;
93  }
94  default:
95  break;
96  }
97  return ret;
98  }
99 };
100 
101 int main(int argc, char **argv)
102 {
103  QApplication a(argc, argv);
104  QMainWindow mw;
105  QWidget *w = new QWidget;
106  QVBoxLayout *vbox = new QVBoxLayout;
107  vbox->addWidget(new QLabel("The blue ellipses should indicate touch point contact patches"));
108  qDebug() << "Touch devices:";
110  QString result;
112  str << (device->type() == QInputDevice::DeviceType::TouchScreen ? "TouchScreen" : "TouchPad")
113  << " \"" << device->name() << "\", max " << device->maximumTouchPoints()
114  << " touch points, capabilities:";
115  const QPointingDevice::Capabilities capabilities = device->capabilities();
116  if (capabilities & QPointingDevice::Capability::Position)
117  str << " Position";
118  if (capabilities & QPointingDevice::Capability::Area)
119  str << " Area";
120  if (capabilities & QPointingDevice::Capability::Pressure)
121  str << " Pressure";
122  if (capabilities & QPointingDevice::Velocity)
123  str << " Velocity";
125  str << " NormalizedPosition";
126  if (capabilities & QInputDevice::DeviceType::MouseEmulation)
127  str << " MouseEmulation";
128  vbox->addWidget(new QLabel(result));
129  qDebug() << " " << result;
130  }
132  view->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
133  QGraphicsScene *scene = new QGraphicsScene(0, 0, 500, 500);
134  TouchableItem *touchableItem = new TouchableItem;
135  scene->addItem(touchableItem);
136  view->setScene(scene);
137  vbox->addWidget(view);
138  w->setLayout(vbox);
139  mw.setCentralWidget(w);
140  QMenu *menu = mw.menuBar()->addMenu("Menu");
141  QAction *clear = new QAction("Clear");
143  qDeleteAll(touchableItem->childItems());
144  });
145  menu->addAction(clear);
146  QAction *ignoreTransform = new QAction("Ignore transformations");
147  QObject::connect(ignoreTransform, &QAction::triggered, [=]() {
148  view->scale(1.5, 1.5);
150  });
151  menu->addAction(ignoreTransform);
152  QAction *quit = new QAction("Quit");
153  quit->setShortcut(QKeySequence::Quit);
155  menu->addAction(quit);
156  mw.show();
157 
158  return a.exec();
159 }
160 
161 
void setPen(const QPen &pen)
void setBrush(const QBrush &brush)
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition: qaction.h:65
void triggered(bool checked=false)
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:66
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
@ TouchEnd
Definition: qcoreevent.h:256
@ TouchUpdate
Definition: qcoreevent.h:255
@ TouchBegin
Definition: qcoreevent.h:254
void accept()
Definition: qcoreevent.h:313
The QEventPoint class provides information about a point in a QPointerEvent.
Definition: qeventpoint.h:56
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
qreal scale() const
QPointF pos() const
QList< QGraphicsItem * > childItems() const
@ ItemIgnoresTransformations
Definition: qgraphicsitem.h:91
void setFlag(GraphicsItemFlag flag, bool enabled=true)
void setAcceptTouchEvents(bool enabled)
virtual bool sceneEvent(QEvent *event)
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
static QList< const QInputDevice * > devices()
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
The QMainWindow class provides a main application window.\inmodule QtWidgets.
Definition: qmainwindow.h:61
void setCentralWidget(QWidget *widget)
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition: qmenu.h:62
void addAction(QAction *action)
Definition: qwidget.cpp:3129
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
constexpr qreal y() const noexcept
Definition: qpoint.h:366
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:62
The QTouchEvent class contains parameters that describe a touch event.
Definition: qevent.h:1020
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void show()
Definition: qwidget.cpp:7825
bool sceneEvent(QEvent *e)
Definition: main.cpp:62
TouchableItem()
Definition: main.cpp:56
int main(int argc, char **argv)
Definition: main.cpp:1
b clear()
QString str
[2]
qDeleteAll(list.begin(), list.end())
double e
@ WA_AcceptTouchEvents
Definition: qnamespace.h:429
@ IgnoreAspectRatio
Definition: qnamespace.h:1212
@ blue
Definition: qnamespace.h:68
@ yellow
Definition: qnamespace.h:71
@ red
Definition: qnamespace.h:66
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
#define qDebug
[1]
Definition: qlogging.h:177
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
QGraphicsScene scene
[0]
QGraphicsEllipseItem * ellipse
QMenu menu
[5]
QQuickView * view
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent