Skip to content

Commit 07cbbe4

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 07cbbe4

File tree

13 files changed

+179
-0
lines changed

13 files changed

+179
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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)
Lines changed: 26 additions & 0 deletions
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
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
54+
setAttribute(Qt::WA_TranslucentBackground);
55+
setWindowModality(Qt::ApplicationModal);
56+
57+
show();
58+
d_ptr->movie->start();
59+
}
60+
61+
void LoadingIndicator::hideLoading()
62+
{
63+
d_ptr->movie->stop();
64+
hide();
65+
}
Lines changed: 18 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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+
};
12.6 KB
Loading

LoadingIndicator/resource.qrc

Lines changed: 5 additions & 0 deletions
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>

0 commit comments

Comments
 (0)