Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mahuifa
QMDemo
提交
e0797973
Q
QMDemo
项目概览
mahuifa
/
QMDemo
通知
1
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
QMDemo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e0797973
编写于
2月 23, 2023
作者:
mahuifa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:增加QThreadPool::run使用示例
上级
9cc0982a
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
323 addition
and
0 deletion
+323
-0
ConcurrentExamples/ConcurrentExamples.pro
ConcurrentExamples/ConcurrentExamples.pro
+1
-0
ConcurrentExamples/UseConcurrent/RunFunction/RunFunction.pro
ConcurrentExamples/UseConcurrent/RunFunction/RunFunction.pro
+51
-0
ConcurrentExamples/UseConcurrent/RunFunction/main.cpp
ConcurrentExamples/UseConcurrent/RunFunction/main.cpp
+11
-0
ConcurrentExamples/UseConcurrent/RunFunction/widget.cpp
ConcurrentExamples/UseConcurrent/RunFunction/widget.cpp
+137
-0
ConcurrentExamples/UseConcurrent/RunFunction/widget.h
ConcurrentExamples/UseConcurrent/RunFunction/widget.h
+35
-0
ConcurrentExamples/UseConcurrent/RunFunction/widget.ui
ConcurrentExamples/UseConcurrent/RunFunction/widget.ui
+73
-0
ConcurrentExamples/UseConcurrent/UseConcurrent.pro
ConcurrentExamples/UseConcurrent/UseConcurrent.pro
+15
-0
未找到文件。
ConcurrentExamples/ConcurrentExamples.pro
浏览文件 @
e0797973
...
...
@@ -13,3 +13,4 @@ TEMPLATE = subdirs
SUBDIRS
+=
UseQThread1
#
Qt
通过【实例化】
QThread
,使用
moveToThread将QObject子类槽函数在子线程中执行
SUBDIRS
+=
UseQThread2
#
Qt
通过子类化,继承
QThread
,重写
run实现子线程
SUBDIRS
+=
UseQThreadPool
#
Qt
使用线程池
QThreadPool
示例
SUBDIRS
+=
UseConcurrent
#
Qt
Concurent
API
使用示例
ConcurrentExamples/UseConcurrent/RunFunction/RunFunction.pro
0 → 100644
浏览文件 @
e0797973
#---------------------------------------------------------------------------------------
#
@
功能:
#
@
编译器:
Desktop
Qt
5.12
.
5
MSVC2017
64
bit
(也支持其它编译器)
#
@
Qt
IDE
:
D
:/
Qt
/
Qt5
.
12.5
/
Tools
/
QtCreator
/
share
/
qtcreator
#
#
@
开发者
mhf
#
@
邮箱
1603291350
@
qq
.
com
#
@
时间
2023
-
02
-
23
13
:
59
:
15
#
@
备注
#---------------------------------------------------------------------------------------
QT
+=
core
gui
concurrent
greaterThan
(
QT_MAJOR_VERSION
,
4
)
:
QT
+=
widgets
CONFIG
+=
c
++
11
DEFINES
+=
QT_DEPRECATED_WARNINGS
SOURCES
+=
\
main
.
cpp
\
widget
.
cpp
HEADERS
+=
\
widget
.
h
FORMS
+=
\
widget
.
ui
#
Default
rules
for
deployment
.
qnx
:
target
.
path
=
/
tmp
/
$$
{
TARGET
}
/
bin
else
:
unix
:
!
android
:
target
.
path
=
/
opt
/
$$
{
TARGET
}
/
bin
!
isEmpty
(
target
.
path
)
:
INSTALLS
+=
target
#
定义程序版本号
VERSION
=
1.0
.
0
DEFINES
+=
APP_VERSION
=
\\\
"$$VERSION\\\"
contains
(
QT_ARCH
,
i386
){
#
使用
32
位编译器
DESTDIR
=
$$
PWD
/../
bin
#
程序输出路径
}
else
{
DESTDIR
=
$$
PWD
/../
bin64
#
使用
64
位编译器
}
#
msvc
>=
2017
编译器使用
utf
-
8
编码
msvc
{
greaterThan
(
QMAKE_MSC_VER
,
1900
){
#
msvc编译器版本大于2015
QMAKE_CFLAGS
+=
/
utf
-
8
QMAKE_CXXFLAGS
+=
/
utf
-
8
}
else
{
message
(
msvc2015及以下版本在代码中使用
【
pragma
execution_character_set
(
"utf-8"
)
】指定编码
)
}
}
ConcurrentExamples/UseConcurrent/RunFunction/main.cpp
0 → 100644
浏览文件 @
e0797973
#include "widget.h"
#include <QApplication>
int
main
(
int
argc
,
char
*
argv
[])
{
QApplication
a
(
argc
,
argv
);
Widget
w
;
w
.
show
();
return
a
.
exec
();
}
ConcurrentExamples/UseConcurrent/RunFunction/widget.cpp
0 → 100644
浏览文件 @
e0797973
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QThread>
#include <QtConcurrent>
using
namespace
QtConcurrent
;
Widget
::
Widget
(
QWidget
*
parent
)
:
QWidget
(
parent
)
,
ui
(
new
Ui
::
Widget
)
{
ui
->
setupUi
(
this
);
this
->
setWindowTitle
(
QString
(
"QtConcurrent::run使用示例--V%1"
).
arg
(
APP_VERSION
));
}
Widget
::~
Widget
()
{
delete
ui
;
}
void
test1
()
{
qDebug
()
<<
"进入测试1函数"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试1函数:"
<<
QThread
::
currentThreadId
();
}
void
test2
(
QString
value1
,
int
value2
)
{
qDebug
()
<<
"进入测试2函数:"
<<
value1
<<
" "
<<
value2
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试2函数:"
<<
QThread
::
currentThreadId
();
}
void
Widget
::
test3
()
{
qDebug
()
<<
"进入测试3函数"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试3函数:"
<<
QThread
::
currentThreadId
();
}
void
Widget
::
test4
()
{
qDebug
()
<<
"进入测试4函数"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试4函数:"
<<
QThread
::
currentThreadId
();
}
/**
* @brief 线程取自全局QThreadPool
*/
void
Widget
::
on_pushButton_clicked
()
{
QFuture
<
void
>
f1
=
run
(
test1
);
// 在新的线程中异步执行无参全局函数
QFuture
<
void
>
f2
=
run
(
test2
,
QString
(
"传入线程的参数"
),
123
);
// 在新的线程中异步执行有参全局函数(注意:参数2不能直接传字符串常量,需要传QString)
QFuture
<
void
>
f3
=
run
(
&
Widget
::
test3
);
// 在新的线程中异步执行静态成员函数
QFuture
<
void
>
f4
=
run
(
this
,
&
Widget
::
test4
);
// 在新的线程中异步执行成员函数
QFuture
<
void
>
f5
=
run
([]()
// 在新线程中执行Lambda表达式
{
qDebug
()
<<
"进入测试Lambda表达式"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试Lambda表达式:"
<<
QThread
::
currentThreadId
();
});
#if 1 // QFuture的用法
if
(
f1
.
isCanceled
())
{
qDebug
()
<<
"并发操作已经取消"
;
}
if
(
f1
.
isFinished
())
{
qDebug
()
<<
"并发操作已经完成"
;
}
if
(
f1
.
isRunning
())
{
qDebug
()
<<
"并发操作正在执行"
;
}
if
(
f1
.
isPaused
())
{
qDebug
()
<<
"并发操作已暂停"
;
}
if
(
f1
.
isStarted
())
{
qDebug
()
<<
"并发操作已经启动"
;
}
#endif
}
int
test5
()
{
qDebug
()
<<
"进入测试5函数"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试5函数:"
<<
QThread
::
currentThreadId
();
return
123
;
}
/**
* @brief 等待线程执行完成,获取返回值
*/
void
Widget
::
on_pushButton_2_clicked
()
{
QFuture
<
int
>
f1
=
run
(
test5
);
// 在新的线程中异步执行无参全局函数
int
ret
=
f1
.
result
();
// 等待线程结束,获取返回值(这个函数会阻塞当前线程)
qDebug
()
<<
"返回结果为:"
<<
ret
;
}
/**
* @brief 等待线程执行完成
*/
void
Widget
::
on_pushButton_3_clicked
()
{
QFuture
<
int
>
f1
=
run
(
test5
);
// 在新的线程中异步执行无参全局函数
f1
.
waitForFinished
();
// 等待线程结束(这个函数会阻塞当前线程)
qDebug
()
<<
"执行完成"
;
}
/**
* @brief 在指定线程池中运行
*/
void
Widget
::
on_pushButton_4_clicked
()
{
QFuture
<
void
>
f1
=
run
(
&
m_pool
,
test1
);
// 在新的线程中异步执行无参全局函数
QFuture
<
void
>
f2
=
run
(
&
m_pool
,
test2
,
QString
(
"传入线程的参数"
),
123
);
// 在新的线程中异步执行有参全局函数(注意:参数2不能直接传字符串常量,需要传QString)
QFuture
<
void
>
f3
=
run
(
&
m_pool
,
&
Widget
::
test3
);
// 在新的线程中异步执行静态成员函数
QFuture
<
void
>
f4
=
run
(
&
m_pool
,
this
,
&
Widget
::
test4
);
// 在新的线程中异步执行成员函数
QFuture
<
void
>
f5
=
run
(
&
m_pool
,
[]()
// 在新线程中执行Lambda表达式
{
qDebug
()
<<
"进入测试Lambda表达式"
;
QThread
::
sleep
(
3
);
qDebug
()
<<
"离开测试Lambda表达式:"
<<
QThread
::
currentThreadId
();
});
}
ConcurrentExamples/UseConcurrent/RunFunction/widget.h
0 → 100644
浏览文件 @
e0797973
#ifndef WIDGET_H
#define WIDGET_H
#include <QThreadPool>
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace
Ui
{
class
Widget
;
}
QT_END_NAMESPACE
class
Widget
:
public
QWidget
{
Q_OBJECT
public:
Widget
(
QWidget
*
parent
=
nullptr
);
~
Widget
();
static
void
test3
();
void
test4
();
private
slots
:
void
on_pushButton_clicked
();
void
on_pushButton_2_clicked
();
void
on_pushButton_3_clicked
();
void
on_pushButton_4_clicked
();
private:
Ui
::
Widget
*
ui
;
QThreadPool
m_pool
;
};
#endif // WIDGET_H
ConcurrentExamples/UseConcurrent/RunFunction/widget.ui
0 → 100644
浏览文件 @
e0797973
<?xml version="1.0" encoding="UTF-8"?>
<ui
version=
"4.0"
>
<class>
Widget
</class>
<widget
class=
"QWidget"
name=
"Widget"
>
<property
name=
"geometry"
>
<rect>
<x>
0
</x>
<y>
0
</y>
<width>
447
</width>
<height>
304
</height>
</rect>
</property>
<property
name=
"windowTitle"
>
<string>
Widget
</string>
</property>
<layout
class=
"QGridLayout"
name=
"gridLayout"
>
<item
row=
"0"
column=
"0"
>
<widget
class=
"QPushButton"
name=
"pushButton"
>
<property
name=
"minimumSize"
>
<size>
<width>
0
</width>
<height>
50
</height>
</size>
</property>
<property
name=
"text"
>
<string>
用法1,线程取自全局QThreadPool
</string>
</property>
</widget>
</item>
<item
row=
"1"
column=
"0"
>
<widget
class=
"QPushButton"
name=
"pushButton_2"
>
<property
name=
"minimumSize"
>
<size>
<width>
0
</width>
<height>
50
</height>
</size>
</property>
<property
name=
"text"
>
<string>
用法2,等待线程执行完成,获取返回值
</string>
</property>
</widget>
</item>
<item
row=
"2"
column=
"0"
>
<widget
class=
"QPushButton"
name=
"pushButton_3"
>
<property
name=
"minimumSize"
>
<size>
<width>
0
</width>
<height>
50
</height>
</size>
</property>
<property
name=
"text"
>
<string>
用法3,等待线程执行完成
</string>
</property>
</widget>
</item>
<item
row=
"3"
column=
"0"
>
<widget
class=
"QPushButton"
name=
"pushButton_4"
>
<property
name=
"minimumSize"
>
<size>
<width>
0
</width>
<height>
50
</height>
</size>
</property>
<property
name=
"text"
>
<string>
用法4,在指定线程池中运行
</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
ConcurrentExamples/UseConcurrent/UseConcurrent.pro
0 → 100644
浏览文件 @
e0797973
#---------------------------------------------------------------------------------------
#
@
功能:
Qt
Concurent
API
使用示例
#
@
编译器:
Desktop
Qt
5.12
.
5
MSVC2017
64
bit
(也支持其它编译器)
#
@
Qt
IDE
:
D
:/
Qt
/
Qt5
.
12.5
/
Tools
/
QtCreator
/
share
/
qtcreator
#
#
@
开发者
mhf
#
@
邮箱
1603291350
@
qq
.
com
#
@
时间
2023
-
02
-
23
13
:
58
:
59
#
@
备注
#---------------------------------------------------------------------------------------
requires
(
qtHaveModule
(
concurrent
))
#
如果条件为
false
,
qmake
在构建时会跳过此项目
TEMPLATE
=
subdirs
SUBDIRS
+=
RunFunction
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录