{"id":124,"date":"2025-05-22T14:31:07","date_gmt":"2025-05-22T06:31:07","guid":{"rendered":"http:\/\/look123.top\/?p=124"},"modified":"2025-05-22T19:25:24","modified_gmt":"2025-05-22T11:25:24","slug":"qt%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/look123.top\/index.php\/2025\/05\/22\/qt%e5%a4%9a%e7%ba%bf%e7%a8%8b\/","title":{"rendered":"Qt\u591a\u7ebf\u7a0b"},"content":{"rendered":"\n<p>Qt\u4e2d\u76ee\u524d\u67094\u79cd\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7ee7\u627fQThread\uff0c\u91cd\u5199run()\u51fd\u6570<\/li>\n\n\n\n<li>\u4f7f\u7528moveToThread\u5c06\u4e00\u4e2a\u7ee7\u627fQObject\u7684\u5b50\u7c7b\u79fb\u81f3\u7ebf\u7a0b\uff0c\u5185\u90e8\u69fd\u51fd\u6570\u5747\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c<\/li>\n\n\n\n<li>\u4f7f\u7528QThreadPool,\u642d\u914dQRunnable\uff08\u7ebf\u7a0b\u6c60\uff09<\/li>\n\n\n\n<li>\u4f7f\u7528QtConcurrent\uff08\u7ebf\u7a0b\u6c60\uff09<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Yihi-1747895323844\"><strong>1\u3001\u4f7f\u7528\u7c7bQThread<\/strong><\/h3>\n\n\n\n<p>\u4ee3\u7801\u5b8c\u5168\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u9700\u8981\u7ee7\u627fQThread\u7c7b\uff0c\u5e76\u4e14\u91cd\u5199\u8be5\u7c7b\u7684run()\u65b9\u6cd5\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u7ebf\u7a0b\u5728\u6267\u884c\u5b8crun()\u51fd\u6570\u4e4b\u540e\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class WorkerThread : public QThread\n{\n    Q_OBJECT\nprotected:\n    void run() Q_DECL_OVERRIDE {\n        QString result;\n        \/* ... here is the expensive or blocking operation ... *\/\n        emit resultReady(result);\n    }\n\nprivate:\nsignals:\n    void resultReady(const QString &amp;s);\n};\n\nvoid MyObject::startWorkInAThread()\n{\n    WorkerThread *workerThread = new WorkerThread(this);\n    connect(workerThread, &amp;WorkerThread::resultReady, this, &amp;MyObject::handleResults);\n    connect(workerThread, &amp;WorkerThread::finished, workerThread, &amp;QObject::deleteLater);\n    workerThread-&gt;start();\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u69fd\u8fdb\u884c\u901a\u4fe1<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u4f8b\u5316\u7684\u5b50\u7c7b\u662f\u5728\u521b\u5efa\u7ebf\u7a0b\u7684\u65e7\u7ebf\u7a0b\u4e2d\uff0c\u4e0d\u662f\u5728\u65b0\u521b\u5efa\u7684\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u56e0\u6b64\uff0c\u4e0e\u8be5\u7ebf\u7a0b\u76f8\u5173\u6240\u6709\u69fd\u961f\u5217\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\uff0c\u90fd\u4f1a\u5728\u521b\u5efa\u7684\u65e7\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\u4e0d\u80fd\u76f4\u63a5\u5728\u65b0\u5efa\u7684\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u69fd\uff0c\u5982\u679c\u9700\u8981\uff0c\u5219\u9700\u8981\u501f\u52a9worker &#8211; object\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u6bcf\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u9700\u8981\u7ee7\u627fQThread\uff0c\u5b9e\u73b0\u4e00\u4e2a\u65b0\u7684\u5b50\u7c7b\uff0c\u4f7f\u7528\u4e0d\u4fbf\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u81ea\u5df1\u7ba1\u7406\u8d44\u6e90\uff0c\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u91ca\u653e\uff0c\u90fd\u9700\u8981\u81ea\u5df1\u624b\u52a8\u7ba1\u7406\uff0c\u5e76\u4e14\uff0c\u9891\u7e41\u7684\u521b\u5efa\u548c\u5220\u9664\u4f1a\u9020\u6210\u6bd4\u8f83\u5927\u7684\u5185\u5b58\u5f00\u9500\u3002<\/li>\n\n\n\n<li>\u5b9e\u4f8b\u5316\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u548crun()\u51fd\u6570\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u56e0\u6b64\uff0c\u5047\u8bbe\u6709\u6210\u5458\u53d8\u91cf\u4e24\u4e2a\u51fd\u6570\u4e2d\u90fd\u80fd\u8bbf\u95ee\uff0c\u5219\u9700\u8981\u6ce8\u610f\uff0c\u591a\u7ebf\u7a0b\u4e2d\u8d44\u6e90\u7684\u8bbf\u95ee\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u9891\u7e41\u7684\u521b\u5efa\u548c\u5220\u9664\uff0c\u5e38\u9a7b\u5185\u5b58\u7684\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wpjj-1747895323918\"><strong>2\u3001\u4f7f\u7528QThread\u7c7bmoveToThread\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u7ee7\u627fQObject\u7684\u7c7b\uff08MyObject\uff09,\u5e76\u628a\u521b\u5efa\u7684MyObject\u7c7b\u901a\u8fc7\u65b9\u6cd5 movetothread \u5230\u521b\u5efa\u597d\u7684\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u7136\u540estart\u5b50\u7ebf\u7a0b\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u3002\u4e3b\u7ebf\u7a0b\u901a\u8fc7\u53d1\u9001\u4fe1\u53f7\uff0c\u8c03\u7528 MyObject \u4e2d\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u5728\u5b50\u7ebf\u7a0b\u4e2d\u7684\u8ba1\u7b97\u3002\u4e0d\u9700\u8981\u7ee7\u627fQThread\uff0c\u4e0d\u9700\u8981\u91cd\u5199run()\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Worker : public QObject\n{\n    Q_OBJECT\n\npublic slots:\n    void doWork(const QString &amp;parameter) {\n        QString result;\n        \/* ... here is the expensive or blocking operation ... *\/\n        emit resultReady(result);\n    }\n\nsignals:\n    void resultReady(const QString &amp;result);\n};\n\nclass Controller : public QObject\n{\n    Q_OBJECT\n    QThread workerThread;\npublic:\n    Controller() {\n        Worker *worker = new Worker;\n        worker-&gt;moveToThread(&amp;workerThread);\n        connect(&amp;workerThread, &amp;QThread::finished, worker, &amp;QObject::deleteLater);\n        connect(this, &amp;Controller::operate, worker, &amp;Worker::doWork);\n        connect(worker, &amp;Worker::resultReady, this, &amp;Controller::handleResults);\n        workerThread.start();\n    }\n    ~Controller() {\n        workerThread.quit();\n        workerThread.wait();\n    }\npublic slots:\n    void handleResults(const QString &amp;);\nsignals:\n    void operate(const QString &amp;);\n};<\/code><\/pre>\n\n\n\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u6709\u5728\u69fd\u4e2d\u6267\u884c\u7684\u64cd\u4f5c\u624d\u662f\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6240\u4ee5\u9700\u8981\u901a\u8fc7\u8fde\u63a5\u4fe1\u53f7\u69fd\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0<\/li>\n\n\n\n<li>\u5982\u679cobject\u5bf9\u8c61\u5b58\u5728\u7236\u5bf9\u8c61\uff0c\u4e0d\u80fd\u5c06\u5176\u79fb\u5230\u5b50\u7ebf\u7a0b\u4e2d\u6267\u884c<\/li>\n\n\n\n<li>\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u65b9\u4fbf\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u4e1a\u52a1\u4e2d<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2OOi-1747895324012\"><strong>3\u3001QThreadPool\u4e0eQRunnable<\/strong><\/h3>\n\n\n\n<p>QRunnable \u662f\u6240\u6709\u53ef\u6267\u884c\u5bf9\u8c61\u7684\u57fa\u7c7b\u3002 QRunnable\u7c7b\u662f\u4e00\u4e2a\u63a5\u53e3, \u7528\u4e8e\u8868\u793a\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\u6216\u4ee3\u7801\u6bb5, \u5177\u4f53\u4efb\u52a1\u5728run() \u51fd\u6570\u5185\u90e8\u5b9e\u73b0\u3002\u5b9e\u73b0\u7684\u8fc7\u7a0b\u8ddfQThread\u7c7b\u4f3c\uff0c\u4e5f\u662f\u9700\u8981\u901a\u8fc7\u7ee7\u627f\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u642d\u914dQThreadPool\uff0c\u4f7f\u5f97\u5176\u5728\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u4ee3\u7801\u3002\u5982\u679cautoDelete()\u8fd4\u56detrue(\u9ed8\u8ba4\u503c)\uff0cQThreadPool\u5c06\u81ea\u52a8\u5220\u9664QRunnable\u3002\u4f7f\u7528setAutoDelete()\u6765\u66f4\u6539\u81ea\u52a8\u5220\u9664\u6807\u8bb0\u3002<\/p>\n\n\n\n<p>QThreadPool\u652f\u6301\u901a\u8fc7\u5728run()\u51fd\u6570\u4e2d\u8c03\u7528QThreadPool::tryStart(this)\u6765\u591a\u6b21\u6267\u884c\u540c\u4e00\u4e2aQRunnable\u3002\u5982\u679cautoDelete\u88ab\u542f\u7528\uff0cQRunnable\u5c06\u5728\u6700\u540e\u4e00\u4e2a\u7ebf\u7a0b\u9000\u51farun\u51fd\u6570\u65f6\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u542f\u7528\u4e86autoDelete\uff0c\u4f7f\u7528\u76f8\u540c\u7684QRunnable\u591a\u6b21\u8c03\u7528QThreadPool::start()\u4f1a\u9020\u6210\u591a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u8d44\u6e90\uff0c\u5f62\u6210\u7ade\u4e89\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Runnable : public QRunnable\n{\n    \/\/Q_OBJECT\t\tQRunnable \u4e0d\u662fQObject\u7684\u5b50\u7c7b\uff0c\u56e0\u6b64\u5728\u8fd9\u513f\u9700\u8981\u6ce8\u610f\npublic:\n\n    ~Runnable()\n    {\n        qDebug() &lt;&lt; \"~Runnable...\"  &lt;&lt; endl;\n    }\n    void run()\n    {\n        qDebug() &lt;&lt; \" Runnable run thread id :\" &lt;&lt; QThread::currentThreadId() &lt;&lt; endl;\n        \/\/...\n    }\n};\n\nvoid MyObject::startWorkInRunable()\n{\n    qDebug() &lt;&lt; \"main thread id :\" &lt;&lt; QThread::currentThreadId() &lt;&lt; endl;\n    Runnable run;\n    QThreadPool::globalInstance()-&gt;start(&amp;run);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u7528\u8d44\u6e90\u7ba1\u7406\uff0cQThreadPool \u542f\u52a8\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u540e\u4f1a\u81ea\u52a8\u91ca\u653e<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u80fd\u4f1a\u5f62\u6210\u591a\u7ebf\u7a0b\u8d44\u6e90\u7ade\u4e89<\/li>\n\n\n\n<li>\u4e0d\u80fd\u4f7f\u7528\u4fe1\u53f7\u69fd\uff08\u4fe1\u53f7\u69fd\u53ea\u80fd\u5728QObject\u4e2d\u4f7f\u7528\uff09<\/li>\n<\/ul>\n\n\n\n<p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>QRunnable\u9002\u7528\u4e8e\u7ebf\u7a0b\u4efb\u52a1\u91cf\u6bd4\u8f83\u5927\uff0c\u9700\u8981\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u3002QRunnable\u80fd\u6709\u6548\u51cf\u5c11\u5185\u5b58\u5f00\u9500\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"GW1n-1747895324088\"><strong>4\u3001\u4f7f\u7528QtConcurrent<\/strong><\/h3>\n\n\n\n<p>Concurrent\u662f\u5e76\u53d1\u7684\u610f\u601d\uff0cQtConcurrent\u662f\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684 API\uff0c\u4f7f\u5f97\u5728\u7f16\u5199\u591a\u7ebf\u7a0b\u7684\u65f6\u5019\uff0c\u65e0\u9700\u4f7f\u7528\u4f4e\u7ea7\u7ebf\u7a0b\u539f\u8bed\uff0c\u5982\u8bfb\u5199\u9501\uff0c\u7b49\u5f85\u6761\u4ef6\u6216\u4fe1\u53f7\u3002\u4f7f\u7528QtConcurrent\u7f16\u5199\u7684\u7a0b\u5e8f\u4f1a\u6839\u636e\u53ef\u7528\u7684\u5904\u7406\u5668\u5185\u6838\u6570\u81ea\u52a8\u8c03\u6574\u4f7f\u7528\u7684\u7ebf\u7a0b\u6570\u3002\u8fd9\u610f\u5473\u7740\u4eca\u540e\u7f16\u5199\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u5728\u672a\u6765\u90e8\u7f72\u5728\u591a\u6838\u7cfb\u7edf\u4e0a\u65f6\u7ee7\u7eed\u6269\u5c55\u3002<\/p>\n\n\n\n<p>QtConcurrent::run\u80fd\u591f\u65b9\u4fbf\u5feb\u6377\u7684\u5c06\u4efb\u52a1\u4e22\u5230\u5b50\u7ebf\u7a0b\u4e2d\u53bb\u6267\u884c\uff0c\u65e0\u9700\u7ee7\u627f\u4efb\u4f55\u7c7b\uff0c\u4e5f\u4e0d\u9700\u8981\u91cd\u5199\u51fd\u6570\uff0c\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>QFuture&lt;T&gt; run(Function function, ...)\nQFuture&lt;T&gt; run(QThreadPool *pool, Function function, ...)   \/\/Qt5.4\u5f15\u5165\n\/\/T\u4e0e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u76f8\u540c\u3002\u975evoid\u7c7b\u578b\u8fd4\u56de\u503c\u53ef\u4ee5\u901a\u8fc7QFuture::result()\u51fd\u6570\u8bbf\u95ee\u3002\n\n#include &lt;QtConcurrent\/QtConCurrent&gt;\n#include &lt;QFuture&gt;\n\nQFuture&lt;void&gt; func = QtConcurrent::run(this, &amp;MyClass::measure, param);\n\nvoid MyClass::measure(const QString&amp; param)\n{\n\t\/\/do something...\n}<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u975e\u5e38\u7b80\u5355\uff0c\u8fd9\u6837\u6211\u4eec\u8bbe\u7f6e\u7684\u51fd\u6570\u4f1a\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u662f\u4eceQThreadPool\u6c60\u4e2d\u83b7\u53d6\u7684\uff0c\u56e0\u6b64\uff0c<strong>\u8be5\u7ebf\u7a0b\u53ef\u80fd\u4e0d\u4f1a\u76f4\u63a5\u6267\u884c\uff08\u5982\u679cQThreadPool\u6c60\u4e2d\u6ca1\u6709\u7a7a\u95f2\u7684\u7ebf\u7a0b\uff09\uff0c\u5f53QThreadPool\u6c60\u4e2d\u6709\u7a7a\u95f2\u7684\u7ebf\u7a0b\u540e\u4f1a\u6267\u884c<\/strong>\uff0c\u6267\u884c\u5b8c\u6210\u540e\u4f1a\u5c06\u7ebf\u7a0b\u8fd8\u7ed9QThreadPool\u3002<\/p>\n\n\n\n<p>QtConcurrent::run()\u8fd4\u56de\u7684QFuture\u4e0d\u652f\u6301\u53d6\u6d88\u3001\u6682\u505c\u6216\u8fdb\u5ea6\u62a5\u544a\u3002\u8fd4\u56de\u7684QFuture\u53ea\u80fd\u7528\u4e8e\u67e5\u8be2\u51fd\u6570\u7684\u8fd0\u884c\/\u5b8c\u6210\u72b6\u6001\u548c\u8fd4\u56de\u503c\u3002\u975evoid\u7c7b\u578b\u8fd4\u56de\u503c\u53ef\u4f7f\u7528QFuture::result()\u8bbf\u95ee\u83b7\u53d6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Qt\u4e2d\u76ee\u524d\u67094\u79cd\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\u3002\u5982\u4e0b\uff1a 1\u3001\u4f7f\u7528\u7c7bQThread \u4ee3\u7801\u5b8c\u5168\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u9700\u8981\u7ee7\u627fQ &#8230; <a title=\"Qt\u591a\u7ebf\u7a0b\" class=\"read-more\" href=\"https:\/\/look123.top\/index.php\/2025\/05\/22\/qt%e5%a4%9a%e7%ba%bf%e7%a8%8b\/\" aria-label=\"\u9605\u8bfb Qt\u591a\u7ebf\u7a0b\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-cpp_qt"],"_links":{"self":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":3,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}