Counting Clicks for Qt Widgets: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
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''' [[How to Use QSettings Bulgarian|Български]] [[How to Use QSettings Spanish|Spanish]] [[How to Use QSettings SimplifiedChinese|简体中文]] [[How to Use QSettings Greek|Ελληνικά]]<br />[[How to Use QSettings Russian|Русский]][[How to Use QSettings Persian|فارسی]]
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]


=How to Use QSettings=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


==QSettings Overview==
'''English''' | [[How_to_Use_QSettings_Bulgarian|Български]] | [[How_to_Use_QSettings_Spanish|Spanish]] | [[How_to_Use_QSettings_SimplifiedChinese|简体中文]] | [[How_to_Use_QSettings_Greek|Ελληνικά]] |<br />[[How_to_Use_QSettings_Russian|Русский]] | [[How_to_Use_QSettings_Persian|فارسی]]


[http://doc.qt.io/qt-5.0/qtcore/qsettings.html 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 <span class="caps">INI</span> text files, system registry on Windows, and in <span class="caps">XML</span> preferences files on Mac OS X.
= How to Use QSettings =


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


==QSettings Object Declaration, Initialization and Construction==
&quot;QSettings&amp;quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html 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.


the above three individual steps will construct a '''QSettings''' object for accessing the settings stored in the file called clickscount.ini,with the <span class="caps">INI</span> file format.
QSettings's values are stored as '''QVariant''', allowing to save different types, such as '''QString''', '''QRect''', and '''QImage''', with the minimum of effort.


==QSettings Class functions used in this example==
== QSettings Object Declaration, Initialization and Construction ==
 
<code>QSettings *settings;<code>
 
</code>Countclicks::Countclicks() : QObject(),settings(NULL)</code>
 
<code>settings = new QSettings(&quot;clickscount.ini&amp;quot;,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 INI file format.
 
== QSettings Class functions used in this example ==


* beginGroup(const QString &amp; prefix )
* beginGroup(const QString &amp; prefix )
Line 25: Line 35:
* 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 41:
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 class’s '''eventFilter(QObject * watched, QEvent * event)''' function.
</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 ===


===countclicks.h===
<code><br />#ifndef COUNTCLICKS_H<br />#define COUNTCLICKS_H<br />#include &lt;QObject&amp;gt;<br />#include &lt;QSettings&amp;gt;


===countclicks.cpp===
class CountClicks : public QObject<br />{<br /> Q_OBJECT


==Usage of CountClicks EventFilter Class==
public:<br /> CountClicks();<br /> ~CountClicks();
 
protected:<br /> bool eventFilter(QObject *objectName, QEvent *event);
 
private:<br />//QSettings Object declaration<br /> QSettings *settings;<br />};<br />#endif // COUNTCLICKS_H<br /></code>
 
=== countclicks.cpp ===
 
<code><br />#include &lt;QMouseEvent&amp;gt;<br />#include &lt;QApplication&amp;gt;
 
#include &quot;countclicks.h&amp;quot;
 
CountClicks::CountClicks() : QObject(),settings(NULL)<br />{<br />}
 
CountClicks::~CountClicks()<br />{<br />}
 
bool CountClicks::eventFilter(QObject '''object, QEvent''' event)<br />{<br /> static int value = 0;
 
//Constructs a QSettings object for accessing the settings stored in the file called &quot;clickscount&amp;quot;, If the file doesn't already exist, it is created with the mentioned file format.Here it is INI text file format.<br />if (!settings)<br /> settings = new QSettings(&quot;clickscount.ini&amp;quot;,QSettings::IniFormat, this);
 
if (event-&gt;type() == QEvent::MouseButtonPress)<br />{<br /> //Groups are useful to avoid typing in the same setting paths over and over.The current settings are stored with the prefix &quot;countclicks&amp;quot; in the clickscount file.<br />settings-&gt;beginGroup(&quot;countclicks&amp;quot;);
 
//Returns the value for the given setting key. If the setting doesn't exist, defaultValue is returned.<br />int value = settings-&gt;value(object-&gt;objectName()).toInt();
 
value = ++value;
 
//Sets the value of setting key to value. If the key already exists, the previous value is overwritten.<br />settings-&gt;setValue(object-&gt;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.<br />settings-&gt;endGroup();
 
// true is returned to stop it being handled furthermore by parent class.<br />return true;<br />}
 
else<br /> {<br /> // all the unhandled events are passed to the base class's eventFilter(QObject * , QEvent * ) function<br /> return QObject::eventFilter(object,event);<br /> }<br />}
 
</code>
 
== Usage of CountClicks EventFilter Class ==


include the countclicks.h
include the countclicks.h
<code> #include &quot;countclicks.h&amp;quot; <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'''<br /> can also be filtered.
Not only MouseButtonPress events are filtered and numbered,but also events like '''QEvent::HoverEnter''','''QEvent::KeyPress'''<br />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 '''cc''' on pushButton.


For example:
For example:
===Categories:===
* [[:Category:Developing-with-Qt|Developing with Qt]]
** [[:Category:Developing-with-Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 10:20, 24 February 2015

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


[toc align_right="yes&quot; depth="3&quot;]

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

How to Use QSettings

QSettings Overview

"QSettings&quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html 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;<code>

Countclicks::Countclicks() : QObject(),settings(NULL)

settings = new QSettings(&quot;clickscount.ini&amp;quot;,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 INI file format.

== QSettings Class functions used in this example ==

* beginGroup(const QString &amp; prefix )

* setValue ( const QString &amp; key, const QVariant &amp; value )

* value ( const QString &amp; key, const QVariant &amp; 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

<br />#ifndef COUNTCLICKS_H<br />#define COUNTCLICKS_H<br />#include &lt;QObject&amp;gt;<br />#include &lt;QSettings&amp;gt;

class CountClicks : public QObject<br />{<br /> Q_OBJECT

public:<br /> CountClicks();<br /> ~CountClicks();

protected:<br /> bool eventFilter(QObject *objectName, QEvent *event);

private:<br />//QSettings Object declaration<br /> QSettings *settings;<br />};<br />#endif // COUNTCLICKS_H<br />

countclicks.cpp

<br />#include &lt;QMouseEvent&amp;gt;<br />#include &lt;QApplication&amp;gt;

#include &quot;countclicks.h&amp;quot;

CountClicks::CountClicks() : QObject(),settings(NULL)<br />{<br />}

CountClicks::~CountClicks()<br />{<br />}

bool CountClicks::eventFilter(QObject '''object, QEvent''' event)<br />{<br /> static int value = 0;

//Constructs a QSettings object for accessing the settings stored in the file called &quot;clickscount&amp;quot;, If the file doesn't already exist, it is created with the mentioned file format.Here it is INI text file format.<br />if (!settings)<br /> settings = new QSettings(&quot;clickscount.ini&amp;quot;,QSettings::IniFormat, this);

if (event-&gt;type() == QEvent::MouseButtonPress)<br />{<br /> //Groups are useful to avoid typing in the same setting paths over and over.The current settings are stored with the prefix &quot;countclicks&amp;quot; in the clickscount file.<br />settings-&gt;beginGroup(&quot;countclicks&amp;quot;);

//Returns the value for the given setting key. If the setting doesn't exist, defaultValue is returned.<br />int value = settings-&gt;value(object-&gt;objectName()).toInt();

value = ++value;

//Sets the value of setting key to value. If the key already exists, the previous value is overwritten.<br />settings-&gt;setValue(object-&gt;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.<br />settings-&gt;endGroup();

// true is returned to stop it being handled furthermore by parent class.<br />return true;<br />}

else<br /> {<br /> // all the unhandled events are passed to the base class's eventFilter(QObject * , QEvent * ) function<br /> return QObject::eventFilter(object,event);<br /> }<br />}

Usage of CountClicks EventFilter Class

include the countclicks.h

 #include &quot;countclicks.h&amp;quot; <code>

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 cc on pushButton.

For example: