Counting Clicks for Qt Widgets: Difference between revisions
No edit summary |
(QSettings doc page pointed to http://doc.qt.io) |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
'''How to find the Number of Clicks received for Qt Widgets using QSettings, re-implementing eventFilter() method''' | '''How to find the Number of Clicks received for Qt Widgets using QSettings, re-implementing eventFilter() method''' | ||
'''English''' [[ | [[Category:Developing with Qt::General]] | ||
[[Category:HowTo]] | |||
[[Category:Snippets]] | |||
[[Category:Tutorial]] | |||
'''English''' | [[How_to_Use_QSettings_Bulgarian|Български]] | [[How_to_Use_QSettings_Spanish|Spanish]] | [[How_to_Use_QSettings_SimplifiedChinese|简体中文]] | [[How_to_Use_QSettings_Greek|Ελληνικά]] | | |||
[[How_to_Use_QSettings_Russian|Русский]] | [[How_to_Use_QSettings_Persian|فارسی]] | |||
= How to Use QSettings = | |||
= | == QSettings Overview == | ||
[http://doc.qt.io QSettings] 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 == | |||
= | <code>QSettings *settings;</code> | ||
<code>Countclicks::Countclicks() : QObject(),settings(NULL)</code> | |||
<code>settings = new QSettings("clickscount.ini",QSettings::IniFormat);</code> | |||
the above three individual steps will construct a '''QSettings''' object for accessing the settings stored in the file called clickscount.ini,with the | 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== | == QSettings Class functions used in this example == | ||
* beginGroup(const QString & | * beginGroup(const QString & prefix ) | ||
* setValue ( const QString & | * setValue ( const QString & key, const QVariant & value ) | ||
* value ( const QString & | * value ( const QString & key, const QVariant & defaultValue = QVariant() ) const | ||
* endGroup () | * endGroup () | ||
==Example== | == 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. | 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. | ||
Line 31: | Line 44: | ||
CountClicks class is inherited from the QObject class.The protected boolean function is re-implemented. | 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 | <code>bool eventFilter ( QObject * watched, QEvent * event ) [virtual] </code> | ||
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 === | |||
<code> | |||
#ifndef COUNTCLICKS_H | |||
#define COUNTCLICKS_H | |||
#include <QObject> | |||
#include <QSettings> | |||
class CountClicks : public QObject | |||
{ | |||
Q_OBJECT | |||
==Usage of CountClicks EventFilter Class== | public: | ||
CountClicks(); | |||
~CountClicks(); | |||
protected: | |||
bool eventFilter(QObject *objectName, QEvent *event); | |||
private: | |||
//QSettings Object declaration | |||
QSettings *settings; | |||
}; | |||
#endif // COUNTCLICKS_H | |||
</code> | |||
=== countclicks.cpp === | |||
<code> | |||
#include <QMouseEvent> | |||
#include <QApplication> | |||
#include "countclicks.h" | |||
CountClicks::CountClicks() : QObject(),settings(NULL) | |||
{ | |||
} | |||
CountClicks::~CountClicks() | |||
{ | |||
} | |||
bool CountClicks::eventFilter(QObject *object, QEvent *event) | |||
{ | |||
static int value = 0; | |||
//Constructs a QSettings object for accessing the settings stored in the file called "clickscount", If the file doesn't already exist, it is created with the mentioned file format.Here it is INI text file format. | |||
if (!settings) | |||
settings = new QSettings("clickscount.ini",QSettings::IniFormat, this); | |||
if (event->type() == QEvent::MouseButtonPress) | |||
{ | |||
//Groups are useful to avoid typing in the same setting paths over and over.The current settings are stored with the prefix "countclicks" in the clickscount file. | |||
settings->beginGroup("countclicks"); | |||
//Returns the value for the given setting key. If the setting doesn't exist, defaultValue is returned. | |||
int value = settings->value(object->objectName()).toInt(); | |||
value = ++value; | |||
//Sets the value of setting key to value. If the key already exists, the previous value is overwritten. | |||
settings->setValue(object->objectName(),value); | |||
//Resets the group to what it was before the corresponding beginGroup() call.If many settings are meant to be saved and restored with the same prefix, then prefixes beginGroup() and endGroup() are used. | |||
settings->endGroup(); | |||
// true is returned to stop it being handled furthermore by parent class. | |||
return true; | |||
} | |||
else | |||
{ | |||
// all the unhandled events are passed to the base class's eventFilter(QObject * , QEvent * ) function | |||
return QObject::eventFilter(object,event); | |||
} | |||
} | |||
</code> | |||
== Usage of CountClicks EventFilter Class == | |||
include the countclicks.h | include the countclicks.h | ||
<code> #include "countclicks.h" </code> | |||
Construct the object of CountClicks class. | Construct the object of CountClicks class. | ||
<code>CountClicks *cc = new CountClicks; </code> | |||
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. | 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''' | 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. | 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 CountClicks class object '''cc''' on pushButton. | ||
For example: | For example: | ||
= | <code>QPushButton *pushButton = new QPushButton(this); | ||
pushButton->installEventFilter(cc);</code> | |||
Latest revision as of 08:37, 16 May 2017
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 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
QSettings *settings;
Countclicks::Countclicks() : QObject(),settings(NULL)
settings = new QSettings("clickscount.ini",QSettings::IniFormat);
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.
bool eventFilter ( QObject * watched, QEvent * event ) [virtual]
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
#ifndef COUNTCLICKS_H
#define COUNTCLICKS_H
#include <QObject>
#include <QSettings>
class CountClicks : public QObject
{
Q_OBJECT
public:
CountClicks();
~CountClicks();
protected:
bool eventFilter(QObject *objectName, QEvent *event);
private:
//QSettings Object declaration
QSettings *settings;
};
#endif // COUNTCLICKS_H
countclicks.cpp
#include <QMouseEvent>
#include <QApplication>
#include "countclicks.h"
CountClicks::CountClicks() : QObject(),settings(NULL)
{
}
CountClicks::~CountClicks()
{
}
bool CountClicks::eventFilter(QObject *object, QEvent *event)
{
static int value = 0;
//Constructs a QSettings object for accessing the settings stored in the file called "clickscount", If the file doesn't already exist, it is created with the mentioned file format.Here it is INI text file format.
if (!settings)
settings = new QSettings("clickscount.ini",QSettings::IniFormat, this);
if (event->type() == QEvent::MouseButtonPress)
{
//Groups are useful to avoid typing in the same setting paths over and over.The current settings are stored with the prefix "countclicks" in the clickscount file.
settings->beginGroup("countclicks");
//Returns the value for the given setting key. If the setting doesn't exist, defaultValue is returned.
int value = settings->value(object->objectName()).toInt();
value = ++value;
//Sets the value of setting key to value. If the key already exists, the previous value is overwritten.
settings->setValue(object->objectName(),value);
//Resets the group to what it was before the corresponding beginGroup() call.If many settings are meant to be saved and restored with the same prefix, then prefixes beginGroup() and endGroup() are used.
settings->endGroup();
// true is returned to stop it being handled furthermore by parent class.
return true;
}
else
{
// all the unhandled events are passed to the base class's eventFilter(QObject * , QEvent * ) function
return QObject::eventFilter(object,event);
}
}
Usage of CountClicks EventFilter Class
include the countclicks.h
#include "countclicks.h"
Construct the object of CountClicks class.
CountClicks *cc = new CountClicks;
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 CountClicks class object cc on pushButton.
For example:
QPushButton *pushButton = new QPushButton(this);
pushButton->installEventFilter(cc);