In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use Qt to achieve threads and timers, the article is very detailed, has a certain reference value, interested friends must read it!
1. Timer QTimer class
The QTimer class provides repetitive and single-shot timers.
The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout () signal to the appropriate slots, and call start (). From then on, it will emit the timeout () signal at constant intervals.
The above paragraph is excerpted from the Qt helper documentation, and we use the QTimer class to define a timer that can be repeated over and over again, or can be stopped only once.
It is also easy to use:
QTimer * timer = new QTimer (this); connect (timer, SIGNAL (timeout ()), this, SLOT (update ()); timer- > start (1000)
Create a QTimer object, connect the signal timeout () to the corresponding slot function, and then call the start () function. Then, at regular intervals, the timer sends out a timeout () signal.
More usage will not be discussed here, you can refer to the official documentation. Such as how to stop, how to make the timer run only once, and so on.
Second, use QTimer1. Misuse
You might do this:
Subclassing QThread, defining a timer in the thread class, and then calling the timer's start () method in the run () method.
TestThread::TestThread (QObject * parent): QThread (parent) {m_pTimer = new QTimer (this); connect (m_pTimer, & QTimer::timeout, this, & TestThread::timeoutSlot);} void TestThread::run () {qDebug () setInterval (1000);} void TestThread::timeoutSlot () {qDebug () setInterval (1000); connect (m_pTimer, & QTimer::timeout, this, & TestThread::timeoutSlot); mtimeter-> start () This- > exec ();}
There are some things to pay attention to:
1. You cannot assign a parent to a timer as follows
M_pTimer = new QTimer (this)
Otherwise, the following warning appears:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is TestThread (0x709d88), parent's thread is QThread (0x6e8be8), current thread is TestThread (0x709d88)
Because the TestThread object is created in the main thread, its QObject child objects must also be created in the main thread. Therefore, the parent object cannot be specified as TestThread.
two。 Event loop exec () must be added
Otherwise, the thread ends immediately and signals finished ().
It is also important to note that, like start, the stop of the timer must be in the TestThread thread, or there will be an error.
Void TestThread::timeoutSlot () {mtimeter-> stop (); qDebug () setInterval (1000); connect (m_pThread, SIGNAL (started ()), m_pTimer, SLOT (start ()); connect (m_pTimer, & QTimer::timeout, this, & ThreadTest::timeOutSlot, Qt::DirectConnection);}
Change the thread of the timer through the moveToThread () method. Do not set a parent class for the timer, otherwise the function will not take effect.
When connecting the signal slot, we have added a parameter, the connection type, to see what values this parameter can have:
Qt::AutoConnection: default value. If the receiver is in the signaling thread, Qt::DirectConnection is used, otherwise Qt::QueuedConnection is used, and the connection type is determined by the signal sent.
Qt::DirectConnection: the slot function is called immediately after the signal is sent, and the slot function is executed in the thread that sent the signal.
Qt::QueuedConnection: when control is returned to the receiver in the event loop of the signal, the slot function is called. The slot function is executed in the recipient's thread.
Going back to our example, first change the thread where the timer is located to the new thread, then connect the signal slot, and the slot function is executed in the thread where the timer is located.
These are all the contents of the article "how to use Qt to implement threads and timers". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.