Handling right button clicks using Qt
m (fixing categories) |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (9 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | [[ | + | {{ArticleMetaData <!-- v1.2 --> |
| + | |sourcecode= [[Media:MBA right click 2.zip]] [[Media:MBA right click 1.zip]] | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') --> | ||
| + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) --> | ||
| + | |platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later --> | ||
| + | |devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase --> | ||
| + | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by= <!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate= 20100122 | ||
| + | |author= [[User:Marcelobarrosalmeida]] | ||
| + | }} | ||
| + | [[Category:Maemo]][[Category:Qt]] | ||
| + | {{Abstract|This article discusses two strategies for handling right button clicks using Qt: the first and simplest being to implement the {{Icode|QObject}} virtual method {{Icode|eventFilter}}, the second being to create a generic event handler for your objects.}} The article covers both strategies and provides working code snippets. It is possible to apply these techniques to Maemo or PC programs, without modifications. However, in Maemo programs it is necessary to press and hold the screen for some instants in order to generate the right click event since a mouse is not available. | ||
| − | + | == Right click by sub-classing == | |
| − | + | ||
| − | == Right click by | + | |
The virtual '''QObject''' method called '''eventFilter''' can be used to handler any event received by the object. It is possible to decide if you want to consume the event (returning false in your reimplementation) or delegate it for further processing by other software layers (calling the event handler of parent class). Moreover, it is necessary to enable the event filter as well, calling the method '''installEventFilter'''. | The virtual '''QObject''' method called '''eventFilter''' can be used to handler any event received by the object. It is possible to decide if you want to consume the event (returning false in your reimplementation) or delegate it for further processing by other software layers (calling the event handler of parent class). Moreover, it is necessary to enable the event filter as well, calling the method '''installEventFilter'''. | ||
| Line 11: | Line 32: | ||
'''rcqlistwidget.h''' | '''rcqlistwidget.h''' | ||
| − | <code cpp> | + | <code cpp-qt> |
#ifndef RCQLISTWIDGET_H | #ifndef RCQLISTWIDGET_H | ||
#define RCQLISTWIDGET_H | #define RCQLISTWIDGET_H | ||
| Line 33: | Line 54: | ||
'''rcqlistwidget.cpp''' | '''rcqlistwidget.cpp''' | ||
| − | <code cpp> | + | <code cpp-qt> |
#include "rcqlistwidget.h" | #include "rcqlistwidget.h" | ||
#include <QMouseEvent> | #include <QMouseEvent> | ||
| Line 64: | Line 85: | ||
When the right button is clicked (or long press in Maemo), the menu is displayed, as showed in the following screenshots: | When the right button is clicked (or long press in Maemo), the menu is displayed, as showed in the following screenshots: | ||
| − | [[ | + | [[File:MBA app maemo rc1.png|400px|Screeenshot]] [[File:MBA app maemo rc2.png|400px|Screeenshot]] |
=== Using Qt Creator UI designer === | === Using Qt Creator UI designer === | ||
| Line 70: | Line 91: | ||
If you want to design your interface using Qt Creator you need to change the base class for the desired components. Click over the component and select "Promote to", type the base class name and press "Add", as indicated in the next picture. | If you want to design your interface using Qt Creator you need to change the base class for the desired components. Click over the component and select "Promote to", type the base class name and press "Add", as indicated in the next picture. | ||
| − | [[ | + | [[File:MBA promote to.png|800px|Screeenshot]] |
== Right click by generic event handler == | == Right click by generic event handler == | ||
| Line 78: | Line 99: | ||
'''rceventhandler.h''' | '''rceventhandler.h''' | ||
| − | <code cpp> | + | <code cpp-qt> |
#ifndef RCEVENTHANDLER_H | #ifndef RCEVENTHANDLER_H | ||
#define RCEVENTHANDLER_H | #define RCEVENTHANDLER_H | ||
| Line 104: | Line 125: | ||
'''rceventhandler.cpp''' | '''rceventhandler.cpp''' | ||
| − | <code cpp> | + | <code cpp-qt> |
#include "rceventhandler.h" | #include "rceventhandler.h" | ||
#include <QContextMenuEvent> | #include <QContextMenuEvent> | ||
| Line 124: | Line 145: | ||
In your main application, create an object from this class and use it as a parameter when calling '''installEventFilter''' method, as in the next code snippet. In this example, only the list widget was used but it is possible to add more object to the event handler. | In your main application, create an object from this class and use it as a parameter when calling '''installEventFilter''' method, as in the next code snippet. In this example, only the list widget was used but it is possible to add more object to the event handler. | ||
| − | <code cpp> | + | <code cpp-qt> |
RightClick::RightClick(QWidget *parent) : QMainWindow(parent), ui(new Ui::RightClick) | RightClick::RightClick(QWidget *parent) : QMainWindow(parent), ui(new Ui::RightClick) | ||
{ | { | ||
| Line 140: | Line 161: | ||
The result is the same, as you can see in the next screenshot taken from Linux desktop. | The result is the same, as you can see in the next screenshot taken from Linux desktop. | ||
| − | [[ | + | [[File:MBA app pc rc.png|400px|Screeenshot]] |
== Source code == | == Source code == | ||
| Line 146: | Line 167: | ||
The two strategies were tested using the next two examples. It was used Qt Creator 4.6.1. | The two strategies were tested using the next two examples. It was used Qt Creator 4.6.1. | ||
| − | * [[Media: | + | * [[Media:MBA right click 1.zip|First strategy]]. |
| − | * [[Media: | + | * [[Media:MBA right click 2.zip|Second strategy]]. |
| + | |||
| + | [[Category:Code Examples]][[Category:How To]][[Category:MeeGo Harmattan]] [[Category:Symbian]] | ||
Latest revision as of 04:17, 11 October 2012
Article Metadata
Code Example
Article
This article discusses two strategies for handling right button clicks using Qt: the first and simplest being to implement the QObject virtual method eventFilter, the second being to create a generic event handler for your objects. The article covers both strategies and provides working code snippets. It is possible to apply these techniques to Maemo or PC programs, without modifications. However, in Maemo programs it is necessary to press and hold the screen for some instants in order to generate the right click event since a mouse is not available.
Contents |
Right click by sub-classing
The virtual QObject method called eventFilter can be used to handler any event received by the object. It is possible to decide if you want to consume the event (returning false in your reimplementation) or delegate it for further processing by other software layers (calling the event handler of parent class). Moreover, it is necessary to enable the event filter as well, calling the method installEventFilter.
This technique may be used for any object derived from QObject. For instance, suppose you have a QListWidget and want to create a popup menu when right button is clicked. The next two files, rcqlistwidget.h and rcqlistwidget.cpp implement the new list widget (called RCQListWidget) and the event handler.
rcqlistwidget.h
#ifndef RCQLISTWIDGET_H
#define RCQLISTWIDGET_H
#include <QListWidget>
class RCQListWidget : public QListWidget
{
Q_OBJECT
public:
explicit RCQListWidget(QWidget *parent = 0);
private:
bool eventFilter(QObject *, QEvent *);
};
#endif // RCQLISTWIDGET_H
rcqlistwidget.cpp
#include "rcqlistwidget.h"
#include <QMouseEvent>
#include <QMenu>
RCQListWidget::RCQListWidget(QWidget *parent) : QListWidget(parent)
{
installEventFilter(this);
}
bool RCQListWidget::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::ContextMenu)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
QMenu *menu = new QMenu(this);
menu->addAction(new QAction("New",this));
menu->addAction(new QAction("Edit",this));
menu->addAction(new QAction("Delete",this));
menu->exec(mouseEvent->globalPos());
return false;
}
else
return QListWidget::eventFilter(obj, event);
}
When the right button is clicked (or long press in Maemo), the menu is displayed, as showed in the following screenshots:
Using Qt Creator UI designer
If you want to design your interface using Qt Creator you need to change the base class for the desired components. Click over the component and select "Promote to", type the base class name and press "Add", as indicated in the next picture.
Right click by generic event handler
If you do not want to sub-class the object, it is possible to handle the right button creating a new QObject subclass just for this purpose. The method eventFilter must be coded for this class. In this example a signal will be used to dispatch the event to the main program. See rceventhandler.h and rceventhandler.cpp implementations below.
rceventhandler.h
#ifndef RCEVENTHANDLER_H
#define RCEVENTHANDLER_H
#include <QObject>
#include <QPoint>
class RCEventHandler : public QObject
{
Q_OBJECT
public:
explicit RCEventHandler(QObject *parent = 0) : QObject(parent) {};
protected:
bool eventFilter(QObject *obj, QEvent *event);
signals:
void send_rightButtonClicked(const QPoint &p);
};
#endif // RCEVENTHANDLER_H
rceventhandler.cpp
#include "rceventhandler.h"
#include <QContextMenuEvent>
#include <QMouseEvent>
bool RCEventHandler::eventFilter(QObject *obj, QEvent *event)
{
// you may handle multiple objects checking "obj" parameter
if (event->type() == QEvent::ContextMenu) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
send_rightButtonClicked(mouseEvent->globalPos());
return true;
}
else
return QObject::eventFilter(obj, event);
}
In your main application, create an object from this class and use it as a parameter when calling installEventFilter method, as in the next code snippet. In this example, only the list widget was used but it is possible to add more object to the event handler.
RightClick::RightClick(QWidget *parent) : QMainWindow(parent), ui(new Ui::RightClick)
{
ui->setupUi(this);
// create the event handler object
RCEventHandler *listEvHandler = new RCEventHandler(this);
// connect signals
connect(listEvHandler, SIGNAL(send_rightButtonClicked(const QPoint&)),
this, SLOT(rightButtonClicked(const QPoint&)));
// install event filter
ui->listWidget->installEventFilter(listEvHandler);
}
The result is the same, as you can see in the next screenshot taken from Linux desktop.
Source code
The two strategies were tested using the next two examples. It was used Qt Creator 4.6.1.

