Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Handling right button clicks using Qt

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Created: marcelobarrosalmeida (22 Jan 2010)
Last edited: hamishwillee (11 Oct 2012)

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.


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.


#include <QListWidget>
class RCQListWidget : public QListWidget
explicit RCQListWidget(QWidget *parent = 0);
bool eventFilter(QObject *, QEvent *);


#include "rcqlistwidget.h"
#include <QMouseEvent>
#include <QMenu>
RCQListWidget::RCQListWidget(QWidget *parent) : QListWidget(parent)
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));
return false;
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:

Screeenshot Screeenshot

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.


#include <QObject>
#include <QPoint>
class RCEventHandler : public QObject
explicit RCEventHandler(QObject *parent = 0) : QObject(parent) {};
bool eventFilter(QObject *obj, QEvent *event);
void send_rightButtonClicked(const QPoint &p);


#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);
return true;
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)
// 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

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.

This page was last modified on 11 October 2012, at 01:17.
175 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.