跳转至

QThread

Multithreading with Qt

QThread

QThread::currentThreadId

QThread多线程

Qt中多线程-线程池的使用-C/C++/qt

There are three variants of QThread::sleep():

Text Only
1
2
3
QThread::sleep(); // seconds
QThread::msleep(); // msec
QThread::usleep(); // usec

Current Thread Id

Text Only
1
qDebug() << "Main Thread ID: " << QThread::currentThreadId();

res

myres.qrc

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<RCC>
    <qresource prefix="/mydice">
        <file>images/d0.jpg</file>
        <file>images/d1.jpg</file>
        <file>images/d2.jpg</file>
        <file>images/d3.jpg</file>
        <file>images/d4.jpg</file>
        <file>images/d5.jpg</file>
        <file>images/d6.jpg</file>
        <file>images/dice.jpg</file>
        <file>images/timg2.jpg</file>
    </qresource>
</RCC>

Problem not using QThread

mainwindow.ui

XML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>280</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>200</y>
      <width>64</width>
      <height>23</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

no response

C++
1
2
3
4
5
6
7
8
9
void MainWindow::on_pushButton_clicked()
{
    int i = 0;
    while(true)
    {
        this->ui->lcdNumber->display(QString::number(i++));
        std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
    }
}

QMutex

QMutex example 1

mythread.h

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>

class MyThread : public QThread
{
protected:
    void run();
public:
    static QMutex mutex;
    static int n;
};

#endif // MYTHREAD_H

mythread.cpp

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "mythread.h"
#include <QDebug>

QMutex MyThread::mutex;
int MyThread::n = 0;

void MyThread::run()
{
    qDebug() << "MyThread::run() begin n= " << n;

    for (int i=0;i<100;++i)
    {
        //MyThread::mutex.lock();
        MyThread::n++;
        //MyThread::mutex.unlock();
        QThread::msleep( 10 );
    }
    qDebug() << "MyThread::run() end n= " << n;
}

main.cpp

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QApplication>
#include "mythread.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MyThread mThread1;
    MyThread mThread2;
    MyThread mThread3;

    mThread1.start();
    mThread2.start();
    mThread3.start();

    mThread1.wait();
    mThread2.wait();
    mThread3.wait();

    qDebug() << "MyThread::n = " << MyThread::n;

    return app.exec();
}

output:

Text Only
1
2
3
4
5
6
7
MyThread::run() begin n=  0
MyThread::run() begin n=  0
MyThread::run() begin n=  0
MyThread::run() end n=  277
MyThread::run() end n=  277
MyThread::run() end n=  277
MyThread::n =  277

QMutex example 2

mythread.h

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>

class MyThread : public QThread
{
protected:
    void run();
public:
    static QMutex mutex;
    static int n;
};

#endif // MYTHREAD_H

mythread.cpp

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "mythread.h"
#include <QDebug>

QMutex MyThread::mutex;
int MyThread::n = 0;

void MyThread::run()
{
    qDebug() << "MyThread::run() begin n= " << n;

    for (int i=0;i<100;++i)
    {
        MyThread::mutex.lock();
        MyThread::n++;
        MyThread::mutex.unlock();
        QThread::msleep( 10 );
    }
    qDebug() << "MyThread::run() end n= " << n;
}

main.cpp

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QApplication>
#include "mythread.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MyThread mThread1;
    MyThread mThread2;
    MyThread mThread3;

    mThread1.start();
    mThread2.start();
    mThread3.start();

    mThread1.wait();
    mThread2.wait();
    mThread3.wait();

    qDebug() << "MyThread::n = " << MyThread::n;

    return app.exec();
}

output:

Text Only
1
2
3
4
5
6
7
MyThread::run() begin n=  0
MyThread::run() begin n=  0
MyThread::run() begin n=  0
MyThread::run() end n=  300
MyThread::run() end n=  300
MyThread::run() end n=  300
MyThread::n =  300