Delay action to wait for user interaction: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Delay action to wait for more user input=
[[Category:Snippets]]


==Usecase==
[toc align_right="yes" depth="3"]


Have you ever created a text filter that can be used to filter a (big) item view so users can get to their content faster? If you did, you probably simply connected <code>QLineEdit::textChanged ( const QString &amp; text ) </code> to a <code>QSortFilterProxyModel::setFilterXXX </code> method, either via your own slot to add some wildcards or wrap the line edit’s string in a regular expression or not. The problem is, now each character typed by the user triggers an update of the whole view, and the proxy model has to go through all the items in your list over and over again. That could get expensive and slow, thereby instead of improving user experience you actually annoy the user…
= Delay action to wait for more user input =


There are many such cases where events can happen in quick succession, and you’d like your application to respond to these changes. On the one hand, it makes sense to respond to as many of those events in one go as possible, but on the other hand you don’t want the update to take too much time after the user is done typing his filter string (or whatever it is you are waiting for).
== Usecase ==


==Enter DelayedExecutionTimer==
Have you ever created a text filter that can be used to filter a (big) item view so users can get to their content faster? If you did, you probably simply connected &lt;code&amp;gt;QLineEdit::textChanged ( const QString &amp; text ) &lt;/code&amp;gt; to a &lt;code&amp;gt;QSortFilterProxyModel::setFilterXXX &lt;/code&amp;gt; method, either via your own slot to add some wildcards or wrap the line edit's string in a regular expression or not. The problem is, now each character typed by the user triggers an update of the whole view, and the proxy model has to go through all the items in your list over and over again. That could get expensive and slow, thereby instead of improving user experience you actually annoy the user…
 
There are many such cases where events can happen in quick succession, and you'd like your application to respond to these changes. On the one hand, it makes sense to respond to as many of those events in one go as possible, but on the other hand you don't want the update to take too much time after the user is done typing his filter string (or whatever it is you are waiting for).
 
== Enter DelayedExecutionTimer ==


One solution to this problem is to use two timers, one short enough so an update will still appear snappy but long enough that it will probably not trigger before there is more input (for instance, if the user is not done typing his filter), and one longer timer that will trigger a set time after the first event happened, so the update will not be delayed forever as more and more events happen that would otherwise delay the update.
One solution to this problem is to use two timers, one short enough so an update will still appear snappy but long enough that it will probably not trigger before there is more input (for instance, if the user is not done typing his filter), and one longer timer that will trigger a set time after the first event happened, so the update will not be delayed forever as more and more events happen that would otherwise delay the update.


The <code>DelayedExecutionTimer</code> class implements this approach, making it very easy to apply in all such cases without mucking about with creating, setting and resetting timers for each case where you need this. <code>DelayedExecutionTimer</code> basically provides one slot <code>trigger()</code> and one signal <code>triggered()</code>. Instead of directly connecting the event to the action (connecting the <code>textChanged()</code> to the <code>setFilter()</code>, for instance), you connect the <code>QLineEdit::textChanged()</code> to <code>DelayedExecutionTimer::trigger()</code>, and <code>DelayedExecutionTimer::triggered()</code> to <code>QSortFilterProxyModel::setFilter…()</code>.
The &lt;code&amp;gt;DelayedExecutionTimer&amp;lt;/code&amp;gt; class implements this approach, making it very easy to apply in all such cases without mucking about with creating, setting and resetting timers for each case where you need this. &lt;code&amp;gt;DelayedExecutionTimer&amp;lt;/code&amp;gt; basically provides one slot &lt;code&amp;gt;trigger()&lt;/code&amp;gt; and one signal &lt;code&amp;gt;triggered()&lt;/code&amp;gt;. Instead of directly connecting the event to the action (connecting the &lt;code&amp;gt;textChanged()&lt;/code&amp;gt; to the &lt;code&amp;gt;setFilter()&lt;/code&amp;gt;, for instance), you connect the &lt;code&amp;gt;QLineEdit::textChanged()&lt;/code&amp;gt; to &lt;code&amp;gt;DelayedExecutionTimer::trigger()&lt;/code&amp;gt;, and &lt;code&amp;gt;DelayedExecutionTimer::triggered()&lt;/code&amp;gt; to &lt;code&amp;gt;QSortFilterProxyModel::setFilter…()&lt;/code&amp;gt;.
 
&lt;code&amp;gt;DelayedExecutionTimer&amp;lt;/code&amp;gt; also provides two more versions of both the trigger() slot and the triggered() signal for convenience. These allow to pass a QString or an int as an argument. The triggered() signal will be emitted without argument, and with both a QString and with an int argument, carrying the last value that was passed in by the corresponding trigger() signal (or a default value if no such value was set). As a last convenience feature, you can set pre- and post- strings, that will be added to the string value before the signal is send. This way, connecting the line edit to the &lt;code&amp;gt;SortFilterProxy&amp;lt;/code&amp;gt; model becomes as simple as this:
 
<code><br />// m_proxy is a QSortFilterProxyModel pointer<br />// m_ui-&gt;leFilter is a QLineEdit representing the filter string to use
 
DelayedExecutionTimer* filterDelay = new DelayedExecutionTimer(this);<br />filterDelay-&gt;setStringPreFix(&quot;'''&quot;);<br />filterDelay-&gt;setStringPostFix(&quot;'''&quot;);<br />connect(m_ui-&gt;leFilter, SIGNAL (textChanged(QString)), filterDelay, SLOT (trigger(QString)));<br />connect(filterDelay, SIGNAL (triggered(QString)), m_proxy, SLOT (setFilterWildcard(QString)));
 
</code>
 
That's all. Of course, you can tweak the timings by either passing in a reasonable minimum and maximum delay in the constructor, or using the setters for these.
 
== Code ==
 
=== delayedexecutiontimer.h ===
 
<code><br />/*<br />Copyright © 2011, Andre Somers<br />All rights reserved.
 
Redistribution and use in source and binary forms, with or without<br />modification, are permitted provided that the following conditions are met:<br /> * Redistributions of source code must retain the above copyright<br /> notice, this list of conditions and the following disclaimer.<br /> * Redistributions in binary form must reproduce the above copyright<br /> notice, this list of conditions and the following disclaimer in the<br /> documentation and/or other materials provided with the distribution.<br /> * Neither the name of the Rathenau Instituut, Andre Somers nor the<br /> names of its contributors may be used to endorse or promote products<br /> derived from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&amp;quot; AND<br />ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED<br />WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE<br />DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS AND/OR RATHENAU INSTITUTE BE LIABLE FOR ANY<br />DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br />(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;<br />LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br />ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT<br />(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS<br />SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br />'''/<br />#ifndef DELAYEDEXECUTIONTIMER_H<br />#define DELAYEDEXECUTIONTIMER_H
<br />#include &lt;QObject&amp;gt;<br />class QTimer;
<br />/'''*<br /> Class to delay execution an action in response to events that may come in bursts<br /> '''/<br />class DelayedExecutionTimer : public QObject<br />{<br />Q_OBJECT<br />public:<br /> DelayedExecutionTimer(int maximumDelay = 1000, int minimumDelay = 250, QObject''' parent = 0);<br /> DelayedExecutionTimer(QObject* parent);
 
/**<br /> The minimum delay is the time the class will wait after being triggered before<br /> emitting the triggered() signals.<br /> '''/<br /> void setMinimumDelay(int delay) {m_minimumDelay = delay;}<br /> int minimumDelay() const {return m_minimumDelay;}<br /> /'''*<br /> The maximum delay is the maximum time that will pass before a call to the trigger() slot<br /> leads to a triggered() signal.<br /> '''/<br /> void setMaximumDelay(int delay) {m_maximumDelay = delay;}<br /> int maximumDelay() const {return m_maximumDelay;}
<br /> /'''*<br /> Sets a string to be attached to the end of the last string set using trigger(QString)<br /> in the triggered(QString) signal. This is useful for, for instance, appending wildcard<br /> characters to a filter string.<br /> '''/<br /> void setStringPostfix(const QString&amp;amp; postfix) {m_postfix = postfix;}<br /> /'''*<br /> Sets a string to be prepended to the beginning of the last string set using trigger(QString)<br /> in the triggered(QString) signal. This is useful for, for instance, appending wildcard<br /> characters to a filter string.<br /> '''/<br /> void setStringPrefix(const QString&amp;amp; prefix) {m_prefix = prefix;}
<br />signals:<br /> void triggered();<br /> void triggered(QString);<br /> void triggered(int);
<br />public slots:<br /> void trigger();<br /> void trigger(QString);<br /> void trigger(int);
<br />private slots:<br /> void timeout();
<br />private:<br /> int m_minimumDelay;<br /> int m_maximumDelay;
<br /> QTimer''' m_minimumTimer;<br /> QTimer* m_maximumTimer;
 
QString m_lastString;<br /> int m_lastInt;
 
QString m_prefix;<br /> QString m_postfix;<br />};
 
#endif // DELAYEDEXECUTIONTIMER_H<br /></code>
 
And here's the CPP code:


<code>DelayedExecutionTimer</code> also provides two more versions of both the trigger() slot and the triggered() signal for convenience. These allow to pass a QString or an int as an argument. The triggered() signal will be emitted without argument, and with both a QString and with an int argument, carrying the last value that was passed in by the corresponding trigger() signal (or a default value if no such value was set). As a last convenience feature, you can set pre- and post- strings, that will be added to the string value before the signal is send. This way, connecting the line edit to the <code>SortFilterProxy</code> model becomes as simple as this:
=== delayedexecutiontimer.cpp ===


That’s all. Of course, you can tweak the timings by either passing in a reasonable minimum and maximum delay in the constructor, or using the setters for these.
<code><br />/*<br />/*<br />Copyright © 2011, Andre Somers<br />All rights reserved.


==Code==
File licence not repeated here for space reasons. See file &quot;delayedexecutiontimer.h&amp;quot; for details of licence.<br />'''/<br />#include &quot;delayedexecutiontimer.h&amp;quot;<br />#include &lt;QTimer&amp;gt;<br />#include &lt;QStringBuilder&amp;gt;
<br />DelayedExecutionTimer::DelayedExecutionTimer(int maximumDelay, int minimumDelay, QObject''' parent):<br /> QObject(parent),<br /> m_minimumDelay(minimumDelay),<br /> m_maximumDelay(maximumDelay),<br /> m_minimumTimer(new QTimer(this)),<br /> m_maximumTimer(new QTimer(this)),<br /> m_lastInt(0)<br />{<br /> connect(m_minimumTimer, SIGNAL (timeout()), SLOT (timeout()));<br /> connect(m_maximumTimer, SIGNAL (timeout()), SLOT (timeout()));<br />}


===delayedexecutiontimer.h===
DelayedExecutionTimer::DelayedExecutionTimer(QObject* parent):<br /> QObject(parent),<br /> m_minimumDelay(250),<br /> m_maximumDelay(1000),<br /> m_minimumTimer(new QTimer(this)),<br /> m_maximumTimer(new QTimer(this)),<br /> m_lastInt(0)<br />{<br /> connect(m_minimumTimer, SIGNAL (timeout()), SLOT (timeout()));<br /> connect(m_maximumTimer, SIGNAL (timeout()), SLOT (timeout()));<br />}


And here’s the <span class="caps">CPP</span> code:
void DelayedExecutionTimer::timeout()<br />{<br /> m_minimumTimer-&gt;stop();<br /> m_maximumTimer-&gt;stop();<br /> emit triggered();<br /> emit triggered(m_prefix % m_lastString % m_postfix);<br /> emit triggered(m_lastInt);<br />}


===delayedexecutiontimer.cpp===
void DelayedExecutionTimer::trigger()<br />{<br /> if (!m_maximumTimer-&gt;isActive()) {<br /> m_maximumTimer-&gt;start(m_maximumDelay);<br /> }<br /> m_minimumTimer-&gt;stop();<br /> m_minimumTimer-&gt;start(m_minimumDelay);<br />}


===Categories:===
void DelayedExecutionTimer::trigger(QString string)<br />{<br /> m_lastString = string;<br /> trigger();<br />}


* [[:Category:snippets|snippets]]
void DelayedExecutionTimer::trigger(int i)<br />{<br /> m_lastInt = i;<br /> trigger();<br />}<br /></code>

Revision as of 09:37, 24 February 2015


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

Delay action to wait for more user input

Usecase

Have you ever created a text filter that can be used to filter a (big) item view so users can get to their content faster? If you did, you probably simply connected <code&gt;QLineEdit::textChanged ( const QString & text ) </code&gt; to a <code&gt;QSortFilterProxyModel::setFilterXXX </code&gt; method, either via your own slot to add some wildcards or wrap the line edit's string in a regular expression or not. The problem is, now each character typed by the user triggers an update of the whole view, and the proxy model has to go through all the items in your list over and over again. That could get expensive and slow, thereby instead of improving user experience you actually annoy the user…

There are many such cases where events can happen in quick succession, and you'd like your application to respond to these changes. On the one hand, it makes sense to respond to as many of those events in one go as possible, but on the other hand you don't want the update to take too much time after the user is done typing his filter string (or whatever it is you are waiting for).

Enter DelayedExecutionTimer

One solution to this problem is to use two timers, one short enough so an update will still appear snappy but long enough that it will probably not trigger before there is more input (for instance, if the user is not done typing his filter), and one longer timer that will trigger a set time after the first event happened, so the update will not be delayed forever as more and more events happen that would otherwise delay the update.

The <code&gt;DelayedExecutionTimer&lt;/code&gt; class implements this approach, making it very easy to apply in all such cases without mucking about with creating, setting and resetting timers for each case where you need this. <code&gt;DelayedExecutionTimer&lt;/code&gt; basically provides one slot <code&gt;trigger()</code&gt; and one signal <code&gt;triggered()</code&gt;. Instead of directly connecting the event to the action (connecting the <code&gt;textChanged()</code&gt; to the <code&gt;setFilter()</code&gt;, for instance), you connect the <code&gt;QLineEdit::textChanged()</code&gt; to <code&gt;DelayedExecutionTimer::trigger()</code&gt;, and <code&gt;DelayedExecutionTimer::triggered()</code&gt; to <code&gt;QSortFilterProxyModel::setFilter…()</code&gt;.

<code&gt;DelayedExecutionTimer&lt;/code&gt; also provides two more versions of both the trigger() slot and the triggered() signal for convenience. These allow to pass a QString or an int as an argument. The triggered() signal will be emitted without argument, and with both a QString and with an int argument, carrying the last value that was passed in by the corresponding trigger() signal (or a default value if no such value was set). As a last convenience feature, you can set pre- and post- strings, that will be added to the string value before the signal is send. This way, connecting the line edit to the <code&gt;SortFilterProxy&lt;/code&gt; model becomes as simple as this:

<br />// m_proxy is a QSortFilterProxyModel pointer<br />// m_ui-&gt;leFilter is a QLineEdit representing the filter string to use

DelayedExecutionTimer* filterDelay = new DelayedExecutionTimer(this);<br />filterDelay-&gt;setStringPreFix(&quot;'''&quot;);<br />filterDelay-&gt;setStringPostFix(&quot;'''&quot;);<br />connect(m_ui-&gt;leFilter, SIGNAL (textChanged(QString)), filterDelay, SLOT (trigger(QString)));<br />connect(filterDelay, SIGNAL (triggered(QString)), m_proxy, SLOT (setFilterWildcard(QString)));

That's all. Of course, you can tweak the timings by either passing in a reasonable minimum and maximum delay in the constructor, or using the setters for these.

Code

delayedexecutiontimer.h

<br />/*<br />Copyright © 2011, Andre Somers<br />All rights reserved.

Redistribution and use in source and binary forms, with or without<br />modification, are permitted provided that the following conditions are met:<br /> * Redistributions of source code must retain the above copyright<br /> notice, this list of conditions and the following disclaimer.<br /> * Redistributions in binary form must reproduce the above copyright<br /> notice, this list of conditions and the following disclaimer in the<br /> documentation and/or other materials provided with the distribution.<br /> * Neither the name of the Rathenau Instituut, Andre Somers nor the<br /> names of its contributors may be used to endorse or promote products<br /> derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&amp;quot; AND<br />ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED<br />WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE<br />DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS AND/OR RATHENAU INSTITUTE BE LIABLE FOR ANY<br />DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br />(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;<br />LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br />ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT<br />(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS<br />SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br />'''/<br />#ifndef DELAYEDEXECUTIONTIMER_H<br />#define DELAYEDEXECUTIONTIMER_H
<br />#include &lt;QObject&amp;gt;<br />class QTimer;
<br />/'''*<br /> Class to delay execution an action in response to events that may come in bursts<br /> '''/<br />class DelayedExecutionTimer : public QObject<br />{<br />Q_OBJECT<br />public:<br /> DelayedExecutionTimer(int maximumDelay = 1000, int minimumDelay = 250, QObject''' parent = 0);<br /> DelayedExecutionTimer(QObject* parent);

/**<br /> The minimum delay is the time the class will wait after being triggered before<br /> emitting the triggered() signals.<br /> '''/<br /> void setMinimumDelay(int delay) {m_minimumDelay = delay;}<br /> int minimumDelay() const {return m_minimumDelay;}<br /> /'''*<br /> The maximum delay is the maximum time that will pass before a call to the trigger() slot<br /> leads to a triggered() signal.<br /> '''/<br /> void setMaximumDelay(int delay) {m_maximumDelay = delay;}<br /> int maximumDelay() const {return m_maximumDelay;}
<br /> /'''*<br /> Sets a string to be attached to the end of the last string set using trigger(QString)<br /> in the triggered(QString) signal. This is useful for, for instance, appending wildcard<br /> characters to a filter string.<br /> '''/<br /> void setStringPostfix(const QString&amp;amp; postfix) {m_postfix = postfix;}<br /> /'''*<br /> Sets a string to be prepended to the beginning of the last string set using trigger(QString)<br /> in the triggered(QString) signal. This is useful for, for instance, appending wildcard<br /> characters to a filter string.<br /> '''/<br /> void setStringPrefix(const QString&amp;amp; prefix) {m_prefix = prefix;}
<br />signals:<br /> void triggered();<br /> void triggered(QString);<br /> void triggered(int);
<br />public slots:<br /> void trigger();<br /> void trigger(QString);<br /> void trigger(int);
<br />private slots:<br /> void timeout();
<br />private:<br /> int m_minimumDelay;<br /> int m_maximumDelay;
<br /> QTimer''' m_minimumTimer;<br /> QTimer* m_maximumTimer;

QString m_lastString;<br /> int m_lastInt;

QString m_prefix;<br /> QString m_postfix;<br />};

#endif // DELAYEDEXECUTIONTIMER_H<br />

And here's the CPP code:

delayedexecutiontimer.cpp

<br />/*<br />/*<br />Copyright © 2011, Andre Somers<br />All rights reserved.

File licence not repeated here for space reasons. See file &quot;delayedexecutiontimer.h&amp;quot; for details of licence.<br />'''/<br />#include &quot;delayedexecutiontimer.h&amp;quot;<br />#include &lt;QTimer&amp;gt;<br />#include &lt;QStringBuilder&amp;gt;
<br />DelayedExecutionTimer::DelayedExecutionTimer(int maximumDelay, int minimumDelay, QObject''' parent):<br /> QObject(parent),<br /> m_minimumDelay(minimumDelay),<br /> m_maximumDelay(maximumDelay),<br /> m_minimumTimer(new QTimer(this)),<br /> m_maximumTimer(new QTimer(this)),<br /> m_lastInt(0)<br />{<br /> connect(m_minimumTimer, SIGNAL (timeout()), SLOT (timeout()));<br /> connect(m_maximumTimer, SIGNAL (timeout()), SLOT (timeout()));<br />}

DelayedExecutionTimer::DelayedExecutionTimer(QObject* parent):<br /> QObject(parent),<br /> m_minimumDelay(250),<br /> m_maximumDelay(1000),<br /> m_minimumTimer(new QTimer(this)),<br /> m_maximumTimer(new QTimer(this)),<br /> m_lastInt(0)<br />{<br /> connect(m_minimumTimer, SIGNAL (timeout()), SLOT (timeout()));<br /> connect(m_maximumTimer, SIGNAL (timeout()), SLOT (timeout()));<br />}

void DelayedExecutionTimer::timeout()<br />{<br /> m_minimumTimer-&gt;stop();<br /> m_maximumTimer-&gt;stop();<br /> emit triggered();<br /> emit triggered(m_prefix % m_lastString % m_postfix);<br /> emit triggered(m_lastInt);<br />}

void DelayedExecutionTimer::trigger()<br />{<br /> if (!m_maximumTimer-&gt;isActive()) {<br /> m_maximumTimer-&gt;start(m_maximumDelay);<br /> }<br /> m_minimumTimer-&gt;stop();<br /> m_minimumTimer-&gt;start(m_minimumDelay);<br />}

void DelayedExecutionTimer::trigger(QString string)<br />{<br /> m_lastString = string;<br /> trigger();<br />}

void DelayedExecutionTimer::trigger(int i)<br />{<br /> m_lastInt = i;<br /> trigger();<br />}<br />