Skip to content

Commit 2eed216

Browse files
committed
更新第四章部分内容“异步任务执行”,新建一个 Qt 的 demo,上传部分图片
1 parent 6e5e210 commit 2eed216

13 files changed

+384
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ node_modules/
3737
.vuepress/.temp/
3838
.vuepress/dist/
3939

40-
.vscode/
40+
.vscode/
41+
.vs/
42+
x64/

.vuepress/public/image/进度条.png

21.7 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "async_progress_bar.h"
2+
3+
async_progress_bar::async_progress_bar(QWidget *parent)
4+
: QMainWindow{ parent }, progress_bar{ new QProgressBar(this) },
5+
button{ new QPushButton("start",this) },button2{ new QPushButton("测试",this) } {
6+
ui.setupUi(this);
7+
8+
progress_bar->setStyleSheet(progress_bar_style);
9+
progress_bar->setRange(0, 1000);
10+
11+
button->setMinimumSize(100, 50);
12+
button->setMaximumWidth(100);
13+
button->setStyleSheet(button_style);
14+
button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
15+
16+
button2->setMinimumSize(100, 50);
17+
button2->setMaximumWidth(100);
18+
button2->setStyleSheet(button_style);
19+
button2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
20+
21+
QVBoxLayout* layout = new QVBoxLayout;
22+
layout->addWidget(progress_bar);
23+
layout->addWidget(button, 0, Qt::AlignHCenter);
24+
layout->addWidget(button2, 0, Qt::AlignHCenter);
25+
// 设置窗口布局为垂直布局管理器
26+
centralWidget()->setLayout(layout);
27+
28+
connect(button, &QPushButton::clicked, this, &async_progress_bar::task);
29+
connect(button2, &QPushButton::clicked, []{
30+
QMessageBox::information(nullptr, "测试", "没有卡界面!");
31+
});
32+
}
33+
34+
async_progress_bar::~async_progress_bar()
35+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
#include <QtWidgets/QMainWindow>
4+
#include <QProgressBar>
5+
#include <QVBoxLayout>
6+
#include <QPushButton>
7+
#include <QMessageBox>
8+
#include <thread>
9+
#include <future>
10+
#include <chrono>
11+
#include <format>
12+
#include "ui_async_progress_bar.h"
13+
14+
using namespace std::chrono_literals;
15+
16+
class async_progress_bar : public QMainWindow{
17+
Q_OBJECT
18+
19+
public:
20+
async_progress_bar(QWidget *parent = nullptr);
21+
~async_progress_bar();
22+
23+
void task(){
24+
future = std::async(std::launch::async, [=] {
25+
QMetaObject::invokeMethod(this, [this] {
26+
// 这里显示的线程 ID 就是主线程,代表这些任务就是在主线程,即 UI 线程执行
27+
QMessageBox::information(nullptr, "线程ID", std::to_string(_Thrd_id()).c_str());
28+
button->setEnabled(false);
29+
progress_bar->setRange(0, 1000);
30+
button->setText("正在执行...");
31+
});
32+
for (int i = 0; i <= 1000; ++i) {
33+
std::this_thread::sleep_for(10ms);
34+
QMetaObject::invokeMethod(this, [this, i] {
35+
progress_bar->setValue(i);
36+
});
37+
}
38+
QMetaObject::invokeMethod(this, [this] {
39+
button->setText("start");
40+
button->setEnabled(true);
41+
});
42+
// 不在 invokeMethod 中获取线程 ID,这里显示的是子线程的ID
43+
auto s = std::to_string(_Thrd_id());
44+
QMetaObject::invokeMethod(this, [=] {
45+
QMessageBox::information(nullptr, "线程ID", s.c_str());
46+
});
47+
});
48+
}
49+
private:
50+
QString progress_bar_style =
51+
"QProgressBar {"
52+
" border: 2px solid grey;"
53+
" border-radius: 5px;"
54+
" background-color: lightgrey;"
55+
" text-align: center;" // 文本居中
56+
" color: #000000;" // 文本颜色
57+
"}"
58+
"QProgressBar::chunk {"
59+
" background-color: #7FFF00;"
60+
" width: 10px;" // 设置每个进度块的宽度
61+
" font: bold 14px;" // 设置进度条文本字体
62+
"}";
63+
QString button_style =
64+
"QPushButton {"
65+
" text-align: center;" // 文本居中
66+
"}";
67+
QProgressBar* progress_bar{};
68+
QPushButton* button{};
69+
QPushButton* button2{};
70+
Ui::async_progress_barClass ui{};
71+
std::future<void>future;
72+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<RCC>
2+
<qresource prefix="async_progress_bar">
3+
</qresource>
4+
</RCC>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34511.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "async_progress_bar", "async_progress_bar.vcxproj", "{F3C92FE7-F905-4FDC-B5C2-333C2565A334}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F3C92FE7-F905-4FDC-B5C2-333C2565A334}.Debug|x64.ActiveCfg = Debug|x64
15+
{F3C92FE7-F905-4FDC-B5C2-333C2565A334}.Debug|x64.Build.0 = Debug|x64
16+
{F3C92FE7-F905-4FDC-B5C2-333C2565A334}.Release|x64.ActiveCfg = Release|x64
17+
{F3C92FE7-F905-4FDC-B5C2-333C2565A334}.Release|x64.Build.0 = Release|x64
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D07A0862-17A7-42C6-83E8-871DAA78A6EE}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<UI version="4.0" >
2+
<class>async_progress_barClass</class>
3+
<widget class="QMainWindow" name="async_progress_barClass" >
4+
<property name="objectName" >
5+
<string notr="true">async_progress_barClass</string>
6+
</property>
7+
<property name="geometry" >
8+
<rect>
9+
<x>0</x>
10+
<y>0</y>
11+
<width>600</width>
12+
<height>400</height>
13+
</rect>
14+
</property>
15+
<property name="windowTitle" >
16+
<string>async_progress_bar</string>
17+
</property> <widget class="QMenuBar" name="menuBar" />
18+
<widget class="QToolBar" name="mainToolBar" />
19+
<widget class="QWidget" name="centralWidget" />
20+
<widget class="QStatusBar" name="statusBar" />
21+
</widget>
22+
<layoutDefault spacing="6" margin="11" />
23+
<pixmapfunction></pixmapfunction>
24+
<resources>
25+
<include location="async_progress_bar.qrc"/>
26+
</resources>
27+
<connections/>
28+
</UI>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{F3C92FE7-F905-4FDC-B5C2-333C2565A334}</ProjectGuid>
15+
<Keyword>QtVS_v304</Keyword>
16+
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0</WindowsTargetPlatformVersion>
17+
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">10.0</WindowsTargetPlatformVersion>
18+
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
19+
</PropertyGroup>
20+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<PlatformToolset>v143</PlatformToolset>
24+
<UseDebugLibraries>true</UseDebugLibraries>
25+
<CharacterSet>Unicode</CharacterSet>
26+
</PropertyGroup>
27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
28+
<ConfigurationType>Application</ConfigurationType>
29+
<PlatformToolset>v143</PlatformToolset>
30+
<UseDebugLibraries>false</UseDebugLibraries>
31+
<WholeProgramOptimization>true</WholeProgramOptimization>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<ImportGroup Condition="Exists('$(QtMsBuild)\qt_defaults.props')">
36+
<Import Project="$(QtMsBuild)\qt_defaults.props" />
37+
</ImportGroup>
38+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
39+
<QtInstall>6.7.0_msvc2019_64</QtInstall>
40+
<QtModules>core;gui;widgets</QtModules>
41+
<QtBuildConfig>debug</QtBuildConfig>
42+
</PropertyGroup>
43+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
44+
<QtInstall>6.7.0_msvc2019_64</QtInstall>
45+
<QtModules>core;gui;widgets</QtModules>
46+
<QtBuildConfig>release</QtBuildConfig>
47+
</PropertyGroup>
48+
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
49+
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
50+
</Target>
51+
<ImportGroup Label="ExtensionSettings" />
52+
<ImportGroup Label="Shared" />
53+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
54+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
55+
<Import Project="$(QtMsBuild)\Qt.props" />
56+
</ImportGroup>
57+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
58+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
59+
<Import Project="$(QtMsBuild)\Qt.props" />
60+
</ImportGroup>
61+
<PropertyGroup Label="UserMacros" />
62+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
63+
</PropertyGroup>
64+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
65+
</PropertyGroup>
66+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<ClCompile>
68+
<LanguageStandard>stdcpp20</LanguageStandard>
69+
</ClCompile>
70+
</ItemDefinitionGroup>
71+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
72+
<ClCompile>
73+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
74+
<WarningLevel>Level3</WarningLevel>
75+
<SDLCheck>true</SDLCheck>
76+
<ConformanceMode>true</ConformanceMode>
77+
</ClCompile>
78+
<Link>
79+
<SubSystem>Windows</SubSystem>
80+
<GenerateDebugInformation>true</GenerateDebugInformation>
81+
</Link>
82+
</ItemDefinitionGroup>
83+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
84+
<ClCompile>
85+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
86+
<WarningLevel>Level3</WarningLevel>
87+
<SDLCheck>true</SDLCheck>
88+
<ConformanceMode>true</ConformanceMode>
89+
<FunctionLevelLinking>true</FunctionLevelLinking>
90+
<IntrinsicFunctions>true</IntrinsicFunctions>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Windows</SubSystem>
94+
<GenerateDebugInformation>false</GenerateDebugInformation>
95+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
96+
<OptimizeReferences>true</OptimizeReferences>
97+
</Link>
98+
</ItemDefinitionGroup>
99+
<ItemGroup>
100+
<QtRcc Include="async_progress_bar.qrc" />
101+
<QtUic Include="async_progress_bar.ui" />
102+
<QtMoc Include="async_progress_bar.h" />
103+
<ClCompile Include="async_progress_bar.cpp" />
104+
<ClCompile Include="main.cpp" />
105+
</ItemGroup>
106+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
107+
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
108+
<Import Project="$(QtMsBuild)\qt.targets" />
109+
</ImportGroup>
110+
<ImportGroup Label="ExtensionTargets">
111+
</ImportGroup>
112+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
<Filter Include="Form Files">
17+
<UniqueIdentifier>{99349809-55BA-4b9d-BF79-8FDBB0286EB3}</UniqueIdentifier>
18+
<Extensions>ui</Extensions>
19+
</Filter>
20+
<Filter Include="Translation Files">
21+
<UniqueIdentifier>{639EADAA-A684-42e4-A9AD-28FC9BCB8F7C}</UniqueIdentifier>
22+
<Extensions>ts</Extensions>
23+
</Filter>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<QtRcc Include="async_progress_bar.qrc">
27+
<Filter>Resource Files</Filter>
28+
</QtRcc>
29+
<QtUic Include="async_progress_bar.ui">
30+
<Filter>Form Files</Filter>
31+
</QtUic>
32+
<QtMoc Include="async_progress_bar.h">
33+
<Filter>Header Files</Filter>
34+
</QtMoc>
35+
<ClCompile Include="async_progress_bar.cpp">
36+
<Filter>Source Files</Filter>
37+
</ClCompile>
38+
</ItemGroup>
39+
<ItemGroup>
40+
<ClCompile Include="main.cpp">
41+
<Filter>Source Files</Filter>
42+
</ClCompile>
43+
</ItemGroup>
44+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup />
4+
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
5+
<QtTouchProperty>
6+
</QtTouchProperty>
7+
</PropertyGroup>
8+
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
9+
<QtTouchProperty>
10+
</QtTouchProperty>
11+
</PropertyGroup>
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "async_progress_bar.h"
2+
#include <QtWidgets/QApplication>
3+
4+
int main(int argc, char *argv[]){
5+
QApplication a(argc, argv);
6+
7+
auto s = std::to_string(_Thrd_id());
8+
QMessageBox::information(nullptr, "主线程ID", s.c_str());
9+
10+
async_progress_bar w;
11+
w.show();
12+
return a.exec();
13+
}

image/第四章/进度条.png

21.7 KB
Loading

0 commit comments

Comments
 (0)