qt5 thread

本文主要围绕Qt多线程展开,提供了QThread使用的相关文章链接,介绍了Qt线程的一些概念和使用方法。同时推荐了多本参考书籍,如《Advanced Qt Programming》等,还提及编译运行项目的注意事项,为学习和使用Qt多线程提供了丰富资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.qt5 thread

好文章

“How to use QThread in the right way (Part 1)”
http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

“How to use QThread in the right way (Part 2)”
http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-2/

https://doc.qt.io/qt-5/qcoreapplication.html#postEvent
https://doc.qt.io/qt-5/signalsandslots.html#signals

http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/
http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html
http://ilearnstuff.blogspot.com/2012/08/when-qthread-isnt-thread.html

good

http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

https://doc.qt.io/qt-5/qthread.html

https://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html

https://doc.qt.io/qt-5/thread-basics.html

https://doc.qt.io/qt-5/thread-basics.html

https://doc-snapshots.qt.io/qt5-5.9/threads-qobject.html

Effective Threading Using Qt
https://nachtimwald.com/2015/05/02/effective-threading-using-qt/

https://doc.qt.io/archives/qq/qq27-responsive-guis.html

https://doc.qt.io/qt-5/examples-threadandconcurrent.html

https://doc.qt.io/qt-5/threads.html

https://wiki.qt.io/Category:Tutorial

Qt::AutoConnection
Qt::DirectConnection
Qt::QueuedConnection
Qt::BlockingQueuedConnection
Qt::UniqueConnection

Qt有一个术语Thread Affinity:
QObject的实例具有线程亲和力,意思是该object接收到queue signal和posted event会运行在该object所在线程。

A QThread object manages one thread of control within the program.
QThreads begin executing in run().
By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.

You can use worker objects by moving them to the thread using QObject::moveToThread().

参考书籍

http://www.qtrac.eu/aqpbook.html

Advanced Qt Programming
Creating Great Software with C++/Qt 4
by Mark Summerfield

这是一本好书!

编译运行项目注意事项:

书中的源代码下载,下载后使用qt creator 5.0.2打开项目文件,比如weathertrayicon.pro,

编辑文件weathertrayicon.pro,在第二行增加widgets。

QT += widgets network xml

如果不增加者一行,编译报错,比如

‘QAbstractButton’ file not found

‘QApplication’ file not found

本书中文译名暂定为:

Advanced Qt Programming: Creating Great Software with C++ and Qt 4

高级Qt编程:用C++和Qt 4创建完美软件

The Thread Support in Qt document is a good starting point into the reference documentation.
Qt comes with several additional examples for QThread and QtConcurrent.
Several good books describe how to work with Qt threads. The most extensive coverage can be found in Advanced Qt Programming by Mark Summerfield, Prentice Hall - roughly 70 of 500 pages cover QThread and QtConcurrent.

11

Chapter 1: Hybrid Desktop/Internet Applications
weathertrayicon
rsspanel
browserwindow
nyrbviewer
matrixquiz
Chapter 2: Audio and Video
moviejingle
playmusic
playvideo
Chapter 3: Model/View Table Models
zipcodes1
zipcodes2
Chapter 4: Model/View Tree Models
timelog1
timelog2
Chapter 5: Model/View Delegates
folderview
timelog1
zipcodes1
Chapter 6: Model/View Views
censusvisualizer
tiledlistview
Chapter 7: Threading with QtConcurrent
image2image
numbergrid
Chapter 8: Threading with QThread
crossfader
findduplicates
Chapter 9: Creating Rich Text Editors
findduplicates
xmledit
timelog1
textedit
Chapter 10: Creating Rich Text Documents
outputsampler
Chapter 11: Creating Graphics/View Windows
petridish1
Chapter 12: Creating Graphics/View Scenes
pagedesigner1
Chapter 13: The Animation and State Machine Frameworks
finddialog
petridish2
pagedesigner2

111

http://www.qtrac.eu/aqpbook.html

Table of Contents
Foreword by Eirik Chambe-Eng
Introduction
Chapter 1. Hybrid Desktop/Internet Applications
Internet-Aware Widgets
Using WebKit
Chapter 2. Audio and Video
Using QSound and QMovie
The Phonon Multimedia Framework
Chapter 3. Model/View Table Models
Qt’s Model/View Architecture
Using QStandardItemModels for Tables
Creating Custom Table Models
Chapter 4. Model/View Tree Models
Using QStandardItemModels for Trees
Creating Custom Tree Models
Chapter 5. Model/View Delegates
Datatype-Specific Editors
Datatype-Specific Delegates
Model-Specific Delegates
Chapter 6. Model/View Views—This is available as a sample chapter—click “Sample Content” and then “Download the sample pages”.
QAbstractItemView Subclasses
Model-Specific Visualizing Views
Chapter 7. Threading with QtConcurrent
Executing Functions in Threads
Filtering and Mapping in Threads
Chapter 8. Threading with QThread
Processing Independent Items
Processing Shared Items
Chapter 9. Creating Rich Text Editors
Introducing QTextDocument
Creating Custom Text Editors
A Rich Text Single Line Editor
Multiline Rich Text Editing
Chapter 10. Creating Rich Text Documents
Exported QTextDocument File Quality
Creating QTextDocuments
Exporting and Printing Documents
Painting Pages
Chapter 11. Creating Graphics/View Windows
The Graphics/View Architecture
Graphics/View Widgets and Layouts
Introducing Graphics Items
Chapter 12. Creating Graphics/View Scenes
Scenes, Items, and Actions
Enhancing QGraphicsView
Creating a Dock Widget Toolbox
Creating Custom Graphics Items
Chapter 13. The Animation and State Machine Frameworks
Introducing the Animation Framework
Introducing the State Machine Framework
Combining Animations and State Machines
Epilogue
Selected Bibliography
Index

参考书籍2

Hands-On High Performance Programming with Qt 5: Build cross-platform applications using concurrency, parallel programming, and memory management

Hands-On High Performance Programming with Qt 5: Build cross-platform applications using concurrency, parallel programming, and memory management

1: UNDERSTANDING PERFORMANT PROGRAMS
2: PROFILING TO FIND BOTTLENECKS
3: DEEP DIVE INTO C++ AND PERFORMANCE
4: USING DATA STRUCTURES AND ALGORITHMS EFFICIENTLY
5: AN IN-DEPTH GUIDE TO CONCURRENCY AND MULTITHREADING
6: PERFORMANCE FAILURES AND HOW TO OVERCOME THEM
7: UNDERSTANDING I/O PERFORMANCE AND OVERCOMING RELATED PROBLEMS
8: OPTIMIZING GRAPHICAL PERFORMANCE
9: OPTIMIZING NETWORK PERFORMANCE
10: QT PERFORMANCE ON EMBEDDED AND MOBILE PLATFORMS
11: TESTING AND DEPLOYING QT APPLICATIONS
What You Will Learn
Understand classic performance best practices
Get to grips with modern hardware architecture and its performance impact
Implement tools and procedures used in performance optimization
Grasp Qt-specific work techniques for graphical user interface (GUI) and platform programming
Make Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP) performant and use the relevant Qt classes
Discover the improvements Qt 5.9 (and the upcoming versions) holds in store
Explore Qt’s graphic engine architecture, strengths, and weaknesses
Authors
Marek Krajewski
Marek has been programming C++ since the mid-90-ties and Qt since 2008. He was involved with Unix and Windows system programming, client-server systems, UMTS network management, enterprise Java, satellite protocol decoding, neural networks, image processing, DVB-T testing appliances, REST APIs and embedded Linux. He holds a Ph.D. in computer science and is currently working as an independent programmer specializing in system programming, communication protocols, GUIs, C++, and Qt. His other interests are off-piste skiing and aikido.

Hands-On High Performance Programming with Qt 5: Build cross-platform applications using concurrency, parallel programming, and memory management Author: Marek Krajewski Pub Date: 2019 ISBN: 978-1789531244 Pages: 384 Language: English Format: EPUB Size: 17 Mb Build efficient and fast Qt applications, target performance problems, and discover solutions to refine your code Achieving efficient code through performance tuning is one of the key challenges faced by many programmers. This book looks at Qt programming from a performance perspective. You’ll explore the performance problems encountered when using the Qt framework and means and ways to resolve them and optimize performance. The book highlights performance improvements and new features released in Qt 5.9, Qt 5.11, and 5.12 (LTE). You’ll master general computer performance best practices and tools, which can help you identify the reasons behind low performance, and the most common performance pitfalls experienced when using the Qt framework. In the following chapters, you’ll explore multithreading and asynchronous programming with C++ and Qt and learn the importance and efficient use of data structures. You’ll also get the opportunity to work through techniques such as memory management and design guidelines, which are essential to improve application performance. Comprehensive sections that cover all these concepts will prepare you for gaining hands-on experience of some of Qt’s most exciting application fields – the mobile and embedded development domains. By the end of this book, you’ll be ready to build Qt applications that are more efficient, concurrent, and performance-oriented in nature What you will learn Understand classic performance best practices Get to grips with modern hardware architecture and its performance impact Implement tools and procedures used in performance optimization Grasp Qt-specific work techniques for graphical user interface (GUI) and platform programming Make Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP) performant and use the relevant Qt classes Discover the improvements Qt 5.9 (and the upcoming versions) holds in store Explore Qt’s graphic engine architecture, strengths, and weaknesses
### 如何在 Qt5 中实现线程与定时器的功能 #### 创建 `QTimerThread` 类 为了展示如何在线程中使用定时器,在头文件 `qtimertread.h` 定义了一个名为 `QTimerThread` 的类,该类继承自 `QObject` 并包含了多个成员变量用于管理线程和计时器[^1]。 ```cpp #ifndef QTIMERTHREAD_H #define QTIMERTHREAD_H #include <QThread> #include <QList> #include <QTimer> #include "tcpclient.h" class QTimerThread : public QObject { Q_OBJECT public: explicit QTimerThread(int iCount, QObject *parent = nullptr); ~QTimerThread(); void createItem(); void startMultThread(); public slots: void update(); private: int m_iThreadCount; QList<QTimer*> m_qTimerList; QList<QThread*> m_threadList; }; #endif // QTIMERTHREAD_H ``` #### 实现 `QTimerThread` 成员函数 接下来是在 `.cpp` 文件中的具体实现部分: ```cpp #include "qtimerthread.h" #include <QDebug> QTimerThread::QTimerThread(int iCount, QObject *parent) : QObject(parent), m_iThreadCount(iCount) {} QTimerThread::~QTimerThread() { qDeleteAll(m_threadList); } void QTimerThread::createItem() { for (int i = 0; i < m_iThreadCount; ++i) { auto timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &QTimerThread::update); auto thread = new QThread(this); timer->moveToThread(thread); m_qTimerList.append(timer); m_threadList.append(thread); } } void QTimerThread::startMultThread() { for (auto* t : m_threadList) { t->start(); } for (auto* tmr : m_qTimerList) { tmr->start(1000); // 设置每秒触发一次超时信号 } } void QTimerThread::update() { qDebug() << "Update called from thread:" << QThread::currentThreadId(); } ``` 这段代码展示了创建并启动多线程的方法,其中每个线程都绑定了一个定时器实例。当定时器到期时会调用 `update()` 函数,并打印当前执行此操作的线程 ID。 #### 关于工具的选择 本项目采用的是 Qt Creator 5.14.2 版本来进行开发工作,而实际应用中可以灵活选用不同版本或其他集成环境来进行类似的编程实践[^2]。 #### 性能考量 值得注意的是,虽然这里选择了 `QThread` 来简化线程管理和同步逻辑,但在某些高性能计算场景下可能需要考虑更低级别的原生线程库如 C++ 标准库提供的 `std::thread` 或者其他第三方高效并发框架。不过对于大多数图形界面应用程序来说,`QThread` 提供的高度封装已经足够满足日常需求[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值