您的位置:首页 > 教育 > 锐评 > 多多视频_南昌seo排名方案_优化人员是什么意思_时事政治2023最新热点事件

多多视频_南昌seo排名方案_优化人员是什么意思_时事政治2023最新热点事件

2025/2/23 9:54:01 来源:https://blog.csdn.net/qq_57049935/article/details/144468034  浏览:    关键词:多多视频_南昌seo排名方案_优化人员是什么意思_时事政治2023最新热点事件
多多视频_南昌seo排名方案_优化人员是什么意思_时事政治2023最新热点事件

        在启动运行qt项目时,加入了启动的动画,让项目感觉更好一点,同时用于一些嵌入式设备,加载动画的同时可以进行项目自检,检查设备是否安全正确,没有出现损伤。

       运行结果

        下图是运行的结果,启动动画加上加载进度条。

        

实现代码 

        下面是实现控件的h文件和c文件,注释详细

        h文件

        

#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H#include <QSplashScreen>
#include <QLabel>
#include <QProgressBar>
#include <QVBoxLayout>
#include <QPainter>class SplashScreen : public QSplashScreen
{Q_OBJECTpublic:// 构造函数,接受一个QPixmap作为背景图片,和一个可选的父窗口指针SplashScreen(const QPixmap &pixmap, QWidget *parent = nullptr);~SplashScreen(); // 析构函数public slots:void setProgress(int value); // 设置进度条的值void setMessage(const QString &message); // 设置消息文本void start(int duration); // 启动进度条,接受一个持续时间作为参数protected:void drawContents(QPainter *painter) override; // 重写绘制内容函数private:QLabel *messageLabel; // 用于显示消息的标签QProgressBar *progressBar; // 进度条
};#endif // SPLASHSCREEN_H
        C文件

        

#include "mysplashscreen.h"
#include <QApplication>
#include <QScreen>
#include <QStyle>
#include <QPainter>// 构造函数
SplashScreen::SplashScreen(const QPixmap &pixmap, QWidget *parent): QSplashScreen(pixmap, Qt::WindowStaysOnTopHint | Qt::SplashScreen)  // 调用QSplashScreen的构造函数,设置窗口标志
{// 设置布局QVBoxLayout *layout = new QVBoxLayout;layout->setContentsMargins(10, 10, 10, 10); // 设置内边距setLayout(layout); // 设置布局// 创建消息标签messageLabel = new QLabel(this);messageLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop); // 设置对齐方式messageLabel->setStyleSheet("color: white;"); // 设置样式表,设置文字颜色为白色layout->addWidget(messageLabel); // 将标签添加到布局中// 创建进度条progressBar = new QProgressBar(this);progressBar->setMaximum(100); // 设置进度条的最大值为100progressBar->setValue(0); // 设置进度条的初始值为0// 设置进度条的样式表,设置颜色progressBar->setStyleSheet("QProgressBar {""   border: 2px solid grey;""   border-radius: 5px;""   text-align: center;""}""QProgressBar::chunk {""   background-color: #05B8CC;""   width: 10px;""   margin: 0.5px;""}");layout->addWidget(progressBar); // 将进度条添加到布局中// 设置启动画面的几何位置和大小setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), qApp->primaryScreen()->geometry()));// 初始化显示show();
}// 析构函数
SplashScreen::~SplashScreen()
{
}// 设置进度条的值
void SplashScreen::setProgress(int value)
{progressBar->setValue(value); // 设置进度条的值repaint(); // 更新显示
}// 设置消息文本
void SplashScreen::setMessage(const QString &message)
{messageLabel->setText(message); // 设置标签的文本repaint(); // 更新显示
}// 启动进度条
void SplashScreen::start(int duration)
{for (int i = 0; i < duration; ++i){int progress = i * 100 / duration; // 计算进度setProgress(progress); // 设置进度setMessage(QString("Loading... %1%").arg(progress)); // 设置消息QApplication::processEvents(); // 处理事件,保持界面响应}
}// 重写绘制内容函数
void SplashScreen::drawContents(QPainter *painter)
{// 调用基类的绘制内容函数QSplashScreen::drawContents(painter);
}
        main函数调用

        通过设置参数,来设置动画启动的时间

#include "TcpClientLogin.h"
#include "mysplashscreen.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>int main(int argc, char *argv[])
{QApplication a(argc, argv);// 创建并显示启动画面QPixmap pixmap(":/res/pic/1.png");SplashScreen *splash = new SplashScreen(pixmap);// 开始启动画面splash->start(10000);QFile file(":/res/qss/style-4.qss");/*QSS文件所在的路径*/file.open(QFile::ReadOnly);QTextStream filetext(&file);QString stylesheet = filetext.readAll();a.setStyleSheet(stylesheet);file.close();MainWindow w;w.show();splash->finish(&w);delete splash;return a.exec();
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com