Counting Clicks for Qt Widgets

From Qt Wiki
Revision as of 15:11, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How to find the Number of Clicks received for Qt Widgets using QSettings, re-implementing eventFilter() method

English Български Spanish 简体中文 Ελληνικά
Русскийفارسی

How to Use QSettings

QSettings Overview

QSettings [qt.io] class provides Persistent and platform-independent application settings. QSettings allows to save and load the previously stored settings(Values) in the form of INI text files, system registry on Windows, and in XML preferences files on Mac OS X.

QSettings’s values are stored as QVariant, allowing to save different types, such as QString, QRect, and QImage, with the minimum of effort.

QSettings Object Declaration, Initialization and Construction

the above three individual steps will construct a QSettings object for accessing the settings stored in the file called clickscount.ini,with the INI file format.

QSettings Class functions used in this example

  • beginGroup(const QString & prefix )
  • setValue ( const QString & key, const QVariant & value )
  • value ( const QString & key, const QVariant & defaultValue = QVariant() ) const
  • endGroup ()

Example

The following example shows how to create a class that handles the events if this class object has been installed as an event filter for the watched object.

CountClicks class is inherited from the QObject class.The protected boolean function is re-implemented.

In the current implementation of this class the event QEvent::MouseButtonPress which includes Qt::LeftButton,Qt::RightButton,Qt::MiddleButton is filtered and handled,so true is returned to stop it being handled furthermore, and all the unhandled events are passed to the base class’s eventFilter(QObject * watched, QEvent * event) function.

countclicks.h

countclicks.cpp

Usage of CountClicks EventFilter Class

include the countclicks.h

Construct the object of CountClicks class.

Filters MouseButtonPress events,and writes them to settings file if this object has been installed as an event filter for desired Qt widget like pushButton in any UserInterface.

Not only MouseButtonPress events are filtered and numbered,but also events like QEvent::HoverEnter,QEvent::KeyPress
can also be filtered.

In the below step all events that are sent to pushButton object are filtered and only MouseButtonPress events are counted and stored for the next session by installing the event filter cc on pushButton.

For example:

Categories: