Skip to content

Commit 52d9f22

Browse files
committed
[新增模块LoadingIndicator]:在项目中添加了加载指示器模块,支持GIF动画显示
- 在CMakeLists.txt中添加了对新模块LoadingIndicator的引用,使其成为项目的一部分。 - 创建了LoadingIndicator模块的CMakeLists.txt文件,定义了模块的源文件和资源,以及如何构建和链接该模块。 - 添加了LoadingIndicator模块的.pro文件,配置了模块的Qt组件和编译选项。 - 实现了LoadingIndicator类,该类负责显示一个GIF动画作为加载指示器。 - 定义了LoadingIndicator类的头文件,提供了类的接口和成员函数声明。 - 编写了main.cc文件,作为LoadingIndicator模块的主函数入口。 - 实现了MainWindow类,提供了一个按钮用于触发加载指示器的显示。 - 定义了MainWindow类的头文件,声明了MainWindow类和相关的槽函数。 - 添加了LoadingIndicator模块的图片资源和GIF动画资源文件。 - 更新了Qt-Examples.pro文件,将LoadingIndicator模块加入到项目子目录中。 - 更新了README.md文件,添加了LoadingIndicator模块的介绍和图片展示。
1 parent d6b8cf9 commit 52d9f22

13 files changed

+176
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_subdirectory(GridViewModel)
4141
add_subdirectory(HttpClient)
4242
add_subdirectory(IconButton)
4343
add_subdirectory(ImageCarousel)
44+
add_subdirectory(LoadingIndicator)
4445
add_subdirectory(LogAsynchronous)
4546
add_subdirectory(MulClient)
4647
add_subdirectory(MulServer)

LoadingIndicator/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(PROJECT_SOURCES loadingindicator.cc loadingindicator.hpp main.cc
2+
mainwindow.cc mainwindow.hpp)
3+
4+
qt_add_resources(SOURCES resource.qrc)
5+
6+
qt_add_executable(LoadingIndicator MANUAL_FINALIZATION ${PROJECT_SOURCES}
7+
${SOURCES})
8+
target_link_libraries(LoadingIndicator PRIVATE Qt6::Widgets)
9+
qt_finalize_executable(LoadingIndicator)

LoadingIndicator/LoadingIndicator.pro

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
QT += core gui
2+
3+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4+
5+
CONFIG += c++17
6+
7+
# You can make your code fail to compile if it uses deprecated APIs.
8+
# In order to do so, uncomment the following line.
9+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
10+
11+
SOURCES += \
12+
loadingindicator.cc \
13+
main.cc \
14+
mainwindow.cc
15+
16+
HEADERS += \
17+
loadingindicator.hpp \
18+
mainwindow.hpp
19+
20+
# Default rules for deployment.
21+
qnx: target.path = /tmp/$${TARGET}/bin
22+
else: unix:!android: target.path = /opt/$${TARGET}/bin
23+
!isEmpty(target.path): INSTALLS += target
24+
25+
RESOURCES += \
26+
resource.qrc

LoadingIndicator/loadingindicator.cc

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "loadingindicator.hpp"
2+
3+
#include <QtWidgets>
4+
5+
class LoadingIndicator::LoadingIndicatorPrivate
6+
{
7+
public:
8+
explicit LoadingIndicatorPrivate(LoadingIndicator *q)
9+
: q_ptr(q)
10+
, movie(new QMovie(":/gif/resource/loading.gif", {}, q_ptr))
11+
{
12+
movie->setCacheMode(QMovie::CacheAll);
13+
}
14+
15+
void setupUI()
16+
{
17+
auto *label = new QLabel(q_ptr);
18+
label->setFixedSize(50, 50);
19+
label->setScaledContents(true);
20+
label->setMovie(movie);
21+
movie->setScaledSize(label->size());
22+
23+
auto *layout = new QHBoxLayout(q_ptr);
24+
layout->addWidget(label, 0, Qt::AlignCenter);
25+
}
26+
27+
LoadingIndicator *q_ptr;
28+
29+
QMovie *movie;
30+
};
31+
32+
LoadingIndicator::LoadingIndicator(QWidget *parent)
33+
: QWidget{parent}
34+
, d_ptr(new LoadingIndicatorPrivate(this))
35+
{
36+
d_ptr->setupUI();
37+
QMetaObject::invokeMethod(this, [this] { showLoading(); }, Qt::QueuedConnection);
38+
}
39+
40+
LoadingIndicator::~LoadingIndicator() {}
41+
42+
void LoadingIndicator::showLoading(QWidget *parent)
43+
{
44+
if (parent == nullptr) {
45+
parent = parentWidget();
46+
}
47+
if (parent == nullptr) {
48+
parent = qApp->activeWindow();
49+
}
50+
setParent(parent);
51+
resize(parent->size());
52+
53+
show();
54+
raise();
55+
d_ptr->movie->start();
56+
}
57+
58+
void LoadingIndicator::hideLoading()
59+
{
60+
d_ptr->movie->stop();
61+
hide();
62+
}

LoadingIndicator/loadingindicator.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <QWidget>
4+
5+
class LoadingIndicator : public QWidget
6+
{
7+
Q_OBJECT
8+
public:
9+
explicit LoadingIndicator(QWidget *parent = nullptr);
10+
~LoadingIndicator();
11+
12+
void showLoading(QWidget *parent = nullptr);
13+
void hideLoading();
14+
15+
private:
16+
class LoadingIndicatorPrivate;
17+
QScopedPointer<LoadingIndicatorPrivate> d_ptr;
18+
};

LoadingIndicator/main.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mainwindow.hpp"
2+
3+
#include <QApplication>
4+
5+
int main(int argc, char *argv[])
6+
{
7+
QApplication a(argc, argv);
8+
MainWindow w;
9+
w.show();
10+
return a.exec();
11+
}

LoadingIndicator/mainwindow.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "mainwindow.hpp"
2+
#include "loadingindicator.hpp"
3+
4+
#include <QtWidgets>
5+
6+
MainWindow::MainWindow(QWidget *parent)
7+
: QMainWindow(parent)
8+
{
9+
auto *button = new QPushButton(tr("Loading Indicator Button: Show 10 seconds"), this);
10+
connect(button, &QPushButton::clicked, this, &MainWindow::onShowLoadingIndicator);
11+
setCentralWidget(button);
12+
13+
resize(300, 185);
14+
}
15+
16+
MainWindow::~MainWindow() {}
17+
18+
void MainWindow::onShowLoadingIndicator()
19+
{
20+
auto *loadingIndicator = new LoadingIndicator(this);
21+
QTimer::singleShot(10000, loadingIndicator, &LoadingIndicator::deleteLater);
22+
}

LoadingIndicator/mainwindow.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <QMainWindow>
4+
5+
class MainWindow : public QMainWindow
6+
{
7+
Q_OBJECT
8+
9+
public:
10+
MainWindow(QWidget *parent = nullptr);
11+
~MainWindow();
12+
13+
private slots:
14+
void onShowLoadingIndicator();
15+
};
11.4 KB
Loading

LoadingIndicator/resource.qrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/gif">
3+
<file>resource/loading.gif</file>
4+
</qresource>
5+
</RCC>

LoadingIndicator/resource/loading.gif

16.8 KB
Loading

Qt-Examples.pro

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SUBDIRS += \
1616
HttpClient \
1717
IconButton \
1818
ImageCarousel \
19+
LoadingIndicator \
1920
LogAsynchronous \
2021
MulClient \
2122
MulServer \

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<img src="ImageCarousel/picture/ImageCarousel.jpg" width="90%" height="90%">
8888
</div>
8989
90+
## [LoadingIndicator](LoadingIndicator/)——加载指示器,支持gif动画
91+
92+
<div align="center">
93+
<img src="LoadingIndicator/picture/LoadingIndicator.jpg" width="35%" height="35%">
94+
</div>
95+
9096
## [LogAsynchronous](LogAsynchronous/)——异步日志,开辟一个线程专门往文件里写日志,前后端分离
9197
9298
1. 日志文件名:应用程序名(appname).时间(time,精确到秒).主机hostname.进程ID(Tid).log(.count),假如一天内写的单个日志大约接近1G,会自动加后缀(.1,.2.3...,以此类推)新建新的日志文件去写,每天0点依然会rollFile;

0 commit comments

Comments
 (0)