add widget mousebuttons example 'mousetester'.
This is an xev-like program. A user clicks a mouse button inside the Window, and the program displays (a) the "raw" button number; (b) the corresponding Qt::MouseButton name; and (c) the type of mouse Event. Task-number: QTBUG-24112 Change-Id: I8a76ff37b5b85639f662706072cc4a2ce490754b Reviewed-by: Casper van Donderen <casper.vandonderen@nokia.com>
This commit is contained in:
parent
e3027377f0
commit
4a80a7ced1
94
doc/src/examples/widgets/mousebuttons.qdoc
Normal file
94
doc/src/examples/widgets/mousebuttons.qdoc
Normal file
@ -0,0 +1,94 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2012 Rick Stockton <rickstockton@reno-computerhelp.com>
|
||||
** Contact: http://www.qt-project.org/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:FDL$
|
||||
** GNU Free Documentation License
|
||||
** Alternatively, this file may be used under the terms of the GNU Free
|
||||
** Documentation License version 1.3 as published by the Free Software
|
||||
** Foundation and appearing in the file included in the packaging of
|
||||
** this file.
|
||||
**
|
||||
** Other Usage
|
||||
** Alternatively, this file may be used in accordance with the terms
|
||||
** and conditions contained in a signed written agreement between you
|
||||
** and Nokia.
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
/*!
|
||||
\example widgets/mousebuttons/buttontester
|
||||
\title Mouse Button Tester
|
||||
|
||||
\brief The 'Mouse Button Tester' example demonstrates how to reimplement
|
||||
mouse events within a custom class. You can also use this program to
|
||||
verify that Qt is actually receiving mouse events from your mouse.
|
||||
|
||||
Many 'gamer' mouse devices are configured with high-numbered "buttons"
|
||||
sending text shortcuts for certain games. With such a mouse, no mouse
|
||||
button events occur: The "mouse" sends keystrokes, and the
|
||||
'Mouse Button Tester' Window will not see the event. Receving no event,
|
||||
it will not repaint the Window with new text describing a \button event.
|
||||
|
||||
And so, in addition to it's use as Qt example code, the program may be
|
||||
useful s a mouse device tester. Note that there is another example
|
||||
muouse buttons example which provides the same function, written in QML.
|
||||
|
||||
This program (the Widget-based example) consists of three classes,
|
||||
in addition to the main() parent program:
|
||||
|
||||
\list
|
||||
\o \c A QPushButton, "Quit".
|
||||
\o \c ButtonTester. This is derived from Qt's TextArea class, for
|
||||
purpose of customizing/re-implementing the mouse and wheel event
|
||||
member functions.
|
||||
\o \c A simple QVBoxLayout layout.
|
||||
\endlist
|
||||
|
||||
First we will review the main program, with it's layout and "Quit"
|
||||
QPushButton. Then we will take a look at the \c ButtonTester class.
|
||||
|
||||
\section1 The Main Program
|
||||
|
||||
Note that the QPushButton, "Quit", is defined directly within the main()
|
||||
program, rather than another class. This is a correct way of defining a
|
||||
"Quit" QPushButton: A "Quit" Button defined inside another
|
||||
class would result in the destructor of that second class being
|
||||
called twice. This "Quit" Button uses the traditional Signal/Slot
|
||||
connection to invoke termination of the QApp, which will properly destroy
|
||||
its child classes \before terminating itself.
|
||||
|
||||
The remainder of the main() program is concerned with defining the layout,
|
||||
and applying a minimum size to the customized ButtonTester.
|
||||
|
||||
\section1 ButtonTester Class Definition
|
||||
|
||||
The \c ButtonTester class inherits from QTextEdit, and listens for
|
||||
mouse events on all possible Qt::MouseButton values. It also listens for
|
||||
wheel events from the mouse, and indicates the direction of wheel motion
|
||||
("up", down", "left", or "right"). It prints short debug messages into
|
||||
the Window, and also on the console QDebug() stream, when mouse button
|
||||
and wheel events occur. Our reimplementation of mousePressEvent(),
|
||||
mouseReleaseEvent(), mouseDoubleClickEvent(), and wheelEvent() "drive"
|
||||
the program; the other functions simply convert the Qt::MouseButton
|
||||
values into text strings.
|
||||
|
||||
You should call the ignore() function on any mouse event (or other event)
|
||||
which your widget-based classes do not use and consume. This function
|
||||
assures that Qt will propagate the event through each parent widget,
|
||||
until it is used or propagated to the Window Manager. (Qt attempts to do
|
||||
this automatically, but it is better programming practice to explicitly
|
||||
invoke the function.)
|
||||
|
||||
\image widgets/mousebutton-buttontester.png
|
||||
*/
|
BIN
doc/src/images/widgets/mousebutton-buttontester.png
Normal file
BIN
doc/src/images/widgets/mousebutton-buttontester.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
172
examples/widgets/mousebuttons/buttontester.cpp
Normal file
172
examples/widgets/mousebuttons/buttontester.cpp
Normal file
@ -0,0 +1,172 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2012 Rick Stockton <rickstockton@reno-computerhelp.com>
|
||||
** Contact: http://www.qt-project.org/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "buttontester.h"
|
||||
|
||||
void ButtonTester::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
int j = ButtonTester::buttonByNumber (e->button());
|
||||
QString result = "Mouse Press: raw button=" + QString::number(j) + " Qt=" + enumNameFromValue(e->button());
|
||||
qDebug() << result;
|
||||
this->setText(result);
|
||||
if (j == 2) {
|
||||
this->repaint();
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonTester::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
int j = ButtonTester::buttonByNumber (e->button());
|
||||
QString result = "Mouse Release: raw button=" + QString::number(j) + " Qt=" + enumNameFromValue(e->button());
|
||||
qDebug() << result;
|
||||
this->setText(result);
|
||||
}
|
||||
|
||||
void ButtonTester::mouseDoubleClickEvent(QMouseEvent *e)
|
||||
{
|
||||
int j = ButtonTester::buttonByNumber (e->button());
|
||||
QString result = "Mouse DoubleClick: raw button=" + QString::number(j) + " Qt=" + enumNameFromValue(e->button());
|
||||
qDebug() << result;
|
||||
this->setText(result);
|
||||
}
|
||||
|
||||
void ButtonTester::wheelEvent (QWheelEvent *e)
|
||||
{
|
||||
QString result;
|
||||
if (e->delta() > 0) {
|
||||
|
||||
if (e->orientation() == Qt::Vertical) {
|
||||
result = "Mouse Wheel Event: UP";
|
||||
} else {
|
||||
result = "Mouse Wheel Event: LEFT";
|
||||
}
|
||||
} else if (e->delta() < 0) {
|
||||
if (e->orientation() == Qt::Vertical) {
|
||||
result = "Mouse Wheel Event: DOWN";
|
||||
} else {
|
||||
result = "Mouse Wheel Event: RIGHT";
|
||||
}
|
||||
}
|
||||
qDebug() << result;
|
||||
this->setText(result);
|
||||
}
|
||||
|
||||
int ButtonTester::buttonByNumber(const Qt::MouseButton button)
|
||||
{
|
||||
if (button == Qt::NoButton) return 0;
|
||||
if (button == Qt::LeftButton) return 1;
|
||||
if (button == Qt::RightButton) return 2;
|
||||
if (button == Qt::MiddleButton) return 3;
|
||||
|
||||
/* Please note that Qt Button #4 corresponds to button #8 on all
|
||||
* platforms which EMULATE wheel events by creating button events
|
||||
* (Button #4 = Scroll Up; Button #5 = Scroll Down; Button #6 = Scroll
|
||||
* Left; and Button #7 = Scroll Right.) This includes X11, with both
|
||||
* Xlib and XCB. So, the "raw button" for "Qt::BackButton" is
|
||||
* usually described as "Button #8".
|
||||
|
||||
* If your platform supports "smooth scrolling", then, for the cases of
|
||||
* Qt::BackButton and higher, this program will show "raw button" with a
|
||||
* value which is too large. Subtract 4 to get the correct button ID for
|
||||
* your platform.
|
||||
*/
|
||||
|
||||
if (button == Qt::BackButton) return 8;
|
||||
if (button == Qt::ForwardButton) return 9;
|
||||
if (button == Qt::TaskButton) return 10;
|
||||
if (button == Qt::ExtraButton4) return 11;
|
||||
if (button == Qt::ExtraButton5) return 12;
|
||||
if (button == Qt::ExtraButton6) return 13;
|
||||
if (button == Qt::ExtraButton7) return 14;
|
||||
if (button == Qt::ExtraButton8) return 15;
|
||||
if (button == Qt::ExtraButton9) return 16;
|
||||
if (button == Qt::ExtraButton10) return 17;
|
||||
if (button == Qt::ExtraButton11) return 18;
|
||||
if (button == Qt::ExtraButton12) return 19;
|
||||
if (button == Qt::ExtraButton13) return 20;
|
||||
if (button == Qt::ExtraButton14) return 21;
|
||||
if (button == Qt::ExtraButton15) return 22;
|
||||
if (button == Qt::ExtraButton16) return 23;
|
||||
if (button == Qt::ExtraButton17) return 24;
|
||||
if (button == Qt::ExtraButton18) return 25;
|
||||
if (button == Qt::ExtraButton19) return 26;
|
||||
if (button == Qt::ExtraButton20) return 27;
|
||||
if (button == Qt::ExtraButton21) return 28;
|
||||
if (button == Qt::ExtraButton22) return 29;
|
||||
if (button == Qt::ExtraButton23) return 30;
|
||||
if (button == Qt::ExtraButton24) return 31;
|
||||
qDebug("QMouseShortcutEntry::addShortcut contained Invalid Qt::MouseButton value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
QString ButtonTester::enumNameFromValue(const Qt::MouseButton button)
|
||||
{
|
||||
if (button == Qt::NoButton) return "NoButton";
|
||||
if (button == Qt::LeftButton) return "LeftButton";
|
||||
if (button == Qt::RightButton) return "RightButton";
|
||||
if (button == Qt::MiddleButton) return "MiddleButton";
|
||||
if (button == Qt::BackButton) return "BackButton";
|
||||
if (button == Qt::ForwardButton) return "ForwardButton";
|
||||
if (button == Qt::TaskButton) return "TaskButton";
|
||||
if (button == Qt::ExtraButton4) return "ExtraButton4";
|
||||
if (button == Qt::ExtraButton5) return "ExtraButton5";
|
||||
if (button == Qt::ExtraButton6) return "ExtraButton6";
|
||||
if (button == Qt::ExtraButton7) return "ExtraButton7";
|
||||
if (button == Qt::ExtraButton8) return "ExtraButton8";
|
||||
if (button == Qt::ExtraButton9) return "ExtraButton9";
|
||||
if (button == Qt::ExtraButton10) return "ExtraButton10";
|
||||
if (button == Qt::ExtraButton11) return "ExtraButton11";
|
||||
if (button == Qt::ExtraButton12) return "ExtraButton12";
|
||||
if (button == Qt::ExtraButton13) return "ExtraButton13";
|
||||
if (button == Qt::ExtraButton14) return "ExtraButton14";
|
||||
if (button == Qt::ExtraButton15) return "ExtraButton15";
|
||||
if (button == Qt::ExtraButton16) return "ExtraButton16";
|
||||
if (button == Qt::ExtraButton17) return "ExtraButton17";
|
||||
if (button == Qt::ExtraButton18) return "ExtraButton18";
|
||||
if (button == Qt::ExtraButton19) return "ExtraButton19";
|
||||
if (button == Qt::ExtraButton20) return "ExtraButton20";
|
||||
if (button == Qt::ExtraButton21) return "ExtraButton21";
|
||||
if (button == Qt::ExtraButton22) return "ExtraButton22";
|
||||
if (button == Qt::ExtraButton23) return "ExtraButton23";
|
||||
if (button == Qt::ExtraButton24) return "ExtraButton24";
|
||||
qDebug("QMouseShortcutEntry::addShortcut contained Invalid Qt::MouseButton value");
|
||||
return "NoButton";
|
||||
}
|
64
examples/widgets/mousebuttons/buttontester.h
Normal file
64
examples/widgets/mousebuttons/buttontester.h
Normal file
@ -0,0 +1,64 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2012 Rick Stockton <rickstockton@reno-computerhelp.com>
|
||||
** Contact: http://www.qt-project.org/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#ifndef BUTTONTESTER_H
|
||||
#define BUTTONTESTER_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QTextEdit>
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <QMouseEvent>
|
||||
#include <QWheelEvent>
|
||||
|
||||
class ButtonTester : public QTextEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
void mouseDoubleClickEvent(QMouseEvent *event);
|
||||
void wheelEvent(QWheelEvent * event);
|
||||
int buttonByNumber(const Qt::MouseButton button);
|
||||
QString enumNameFromValue(const Qt::MouseButton button);
|
||||
};
|
||||
|
||||
#endif // BUTTONTESTER_H
|
18
examples/widgets/mousebuttons/buttontester.pro
Normal file
18
examples/widgets/mousebuttons/buttontester.pro
Normal file
@ -0,0 +1,18 @@
|
||||
TEMPLATE = app
|
||||
|
||||
TARGET = buttontester
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += \
|
||||
main.cpp\
|
||||
buttontester.cpp \
|
||||
|
||||
HEADERS += \
|
||||
buttontester.h \
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/examples/widgets/mousebuttons
|
||||
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS buttontester.pro
|
||||
sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/examples/widgets/mousebuttons
|
||||
INSTALLS += target sources
|
||||
QT += core widgets
|
66
examples/widgets/mousebuttons/main.cpp
Normal file
66
examples/widgets/mousebuttons/main.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2012 Rick Stockton <rickstockton@reno-computerhelp.com>
|
||||
** Contact: http://www.qt-project.org/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "buttontester.h"
|
||||
#include <QtGui>
|
||||
|
||||
int main(int argv, char **args)
|
||||
{
|
||||
QApplication app(argv, args);
|
||||
|
||||
ButtonTester *testArea = new ButtonTester;
|
||||
testArea->setMinimumSize(500, 350);
|
||||
testArea->setText("To test your mouse with Qt, press buttons in this area.\nYou may also scroll or tilt your mouse wheel.");
|
||||
QPushButton *quitButton = new QPushButton("Quit");
|
||||
|
||||
QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
|
||||
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
layout->addWidget(testArea);
|
||||
layout->addWidget(quitButton);
|
||||
|
||||
QWidget window;
|
||||
window.setLayout(layout);
|
||||
window.setWindowTitle("Mouse Button Tester");
|
||||
window.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
@ -12,6 +12,7 @@ SUBDIRS = analogclock \
|
||||
imageviewer \
|
||||
lineedits \
|
||||
movie \
|
||||
mousebuttons \
|
||||
orientation \
|
||||
scribble \
|
||||
shapedclock \
|
||||
|
Loading…
Reference in New Issue
Block a user