Delay action to wait for user interaction: Difference between revisions
No edit summary |
Waldyrious (talk | contribs) (fix variable name (typo in previous edit)) |
||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
= | {{LangSwitch}} | ||
== Usecase == | |||
Have you ever created a text filter that can be used to filter a (big) list of items? If you did, you probably simply connected <code>QLineEdit::textChanged ( const QString & text ) </code> to a method like <code>YourClass::filterEntries( const QString & filterText )</code> The problem is, now each character typed by the user triggers an update of the whole view, and the class has to go through all the items in your list over and over again. That could get expensive and slow, thereby instead of improving the 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 their filter string (or whatever it is you are waiting for). | |||
== Solution == | |||
The simplest solution to this problem is to use a {{class|QTimer}}. This allows you to, for instance, implement a text filter that waits for the user to stop typing and then run the query. Below is example code that shows how to implement this: | |||
First, add a <tt>{{class|QTimer}} *m_typingTimer</tt>, and a <tt>{{class|QString}} m_filterText</tt> as private member variables for the class. | |||
Initialize the QTimer in the class constructor: | |||
<code> | |||
m_typingTimer = new QTimer( this ); | |||
m_typingTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started | |||
</code> | |||
Then, create a slot where the query will be performed, and connect the timer to it: | |||
<code> | |||
connect( m_typingTimer, &QTimer::timeout, this, &YourClass::filterEntries ); | |||
void YourClass::filterEntries() | |||
{ | |||
// The actual filtering code goes here, using the string stored in the m_filterText class variable | |||
} | |||
</code> | |||
Finally, create a <tt>YourClass::onTextEdited</tt> slot that stores the newly edited filter text and starts the countdown to call the filtering method: | |||
<code> | |||
connect( m_textEdit, QLineEdit::textChanged, this, &YourClass::onTextEdited ); | |||
= | void YourClass::onTextEdited( const QString & newText ) | ||
{ | |||
m_filterText = newText; | |||
m_typingTimer->start( 100 ); // This will fire filterEntries after 100 ms. | |||
// If the user types something before it fires, the timer restarts counting | |||
} | |||
</code> | |||
:''Note: the code above is adapted from a [http://stackoverflow.com/a/21945529/266309 StackOverflow answer] by [http://stackoverflow.com/users/2502409/nazar554 Nazar554].'' | |||
Latest revision as of 11:04, 25 July 2016
En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh
Usecase
Have you ever created a text filter that can be used to filter a (big) list of items? If you did, you probably simply connected
QLineEdit::textChanged ( const QString & text )
to a method like
YourClass::filterEntries( const QString & filterText )
The problem is, now each character typed by the user triggers an update of the whole view, and the class has to go through all the items in your list over and over again. That could get expensive and slow, thereby instead of improving the 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 their filter string (or whatever it is you are waiting for).
Solution
The simplest solution to this problem is to use a QTimer. This allows you to, for instance, implement a text filter that waits for the user to stop typing and then run the query. Below is example code that shows how to implement this:
First, add a QTimer *m_typingTimer, and a QString m_filterText as private member variables for the class. Initialize the QTimer in the class constructor:
m_typingTimer = new QTimer( this );
m_typingTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started
Then, create a slot where the query will be performed, and connect the timer to it:
connect( m_typingTimer, &QTimer::timeout, this, &YourClass::filterEntries );
void YourClass::filterEntries()
{
// The actual filtering code goes here, using the string stored in the m_filterText class variable
}
Finally, create a YourClass::onTextEdited slot that stores the newly edited filter text and starts the countdown to call the filtering method:
connect( m_textEdit, QLineEdit::textChanged, this, &YourClass::onTextEdited );
void YourClass::onTextEdited( const QString & newText )
{
m_filterText = newText;
m_typingTimer->start( 100 ); // This will fire filterEntries after 100 ms.
// If the user types something before it fires, the timer restarts counting
}
- Note: the code above is adapted from a StackOverflow answer by Nazar554.