loadingWidget.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // loadingwidget.cpp
  2. #include "loadingwidget.h"
  3. #include <QApplication>
  4. #include <QLabel>
  5. #include <QMovie>
  6. #include <QResizeEvent>
  7. #include <QScreen>
  8. #include <QVBoxLayout>
  9. #include "mainwindow.h"
  10. LoadingWidget* LoadingWidget::m_instance = nullptr;
  11. LoadingWidget::LoadingWidget(QWidget* parent) : QWidget(parent) { initUI(); }
  12. LoadingWidget* LoadingWidget::instance() {
  13. if (!m_instance) {
  14. m_instance = new LoadingWidget();
  15. }
  16. return m_instance;
  17. }
  18. void LoadingWidget::showLoading(QWidget* parent, const QString& text) {
  19. LoadingWidget* instance = LoadingWidget::instance();
  20. // 如果提供了parent,则设置parent
  21. // if (parent) {
  22. // instance->setParent(parent);
  23. // }
  24. instance->m_textLabel->setText(text);
  25. instance->m_movie->start();
  26. instance->updatePosition();
  27. instance->show();
  28. instance->raise();
  29. }
  30. void LoadingWidget::hideLoading() {
  31. if (m_instance) {
  32. m_instance->m_movie->stop();
  33. m_instance->hide();
  34. }
  35. }
  36. void LoadingWidget::initUI() {
  37. // 设置窗口属性
  38. setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow);
  39. setAttribute(Qt::WA_TranslucentBackground);
  40. setAttribute(Qt::WA_ShowWithoutActivating);
  41. // 创建动画
  42. m_movie = new QMovie(":/icons/icons/loading.gif");
  43. m_loadingLabel = new QLabel(this);
  44. m_loadingLabel->setMovie(m_movie);
  45. m_loadingLabel->setAlignment(Qt::AlignCenter);
  46. // 创建文本标签
  47. m_textLabel = new QLabel(this);
  48. m_textLabel->setAlignment(Qt::AlignCenter);
  49. // 布局
  50. QVBoxLayout* layout = new QVBoxLayout(this);
  51. layout->addWidget(m_loadingLabel);
  52. layout->addWidget(m_textLabel);
  53. layout->setSpacing(10);
  54. layout->setContentsMargins(50, 80, 50, 50);
  55. // 样式
  56. setStyleSheet("background-color: rgba(255, 255, 255, 0.8); border-radius: 0px;");
  57. m_textLabel->setStyleSheet("color: white; font-size: 16px;");
  58. // 初始隐藏
  59. hide();
  60. }
  61. void LoadingWidget::resizeEvent(QResizeEvent* event) {
  62. QWidget::resizeEvent(event);
  63. updatePosition();
  64. }
  65. void LoadingWidget::updatePosition() {
  66. if (parentWidget()) {
  67. // 居中显示在父窗口
  68. QRect parentRect = parentWidget()->rect();
  69. resize(parentRect.size());
  70. move(parentRect.center() - rect().center());
  71. } else {
  72. // 如果没有父窗口,居中显示在屏幕
  73. QRect screenGeometry = QApplication::primaryScreen()->geometry();
  74. resize(1024, 768);
  75. move(screenGeometry.center() - rect().center());
  76. }
  77. }
  78. void LoadingWidget::init(QStackedWidget* stackedWidget) {
  79. LoadingWidget* instance = LoadingWidget::instance();
  80. if (stackedWidget) {
  81. instance->setParent(stackedWidget);
  82. instance->updatePosition();
  83. }
  84. }