QT-好看的启动画面使用

QT-好看的启动画面使用

  • 前言
  • 一、显示效果
  • 二、关键程序
    • 1.QtExScreen类的使用
    • 2.main函数中使用
  • 三、程序链接

QT-好看的启动画面使用_第1张图片

前言

通常情况下,如果我们软件初始化过程中要等待的时间比较长,那么为了更好的体验感,我们可以通过添加软件启动画面来设置。
但是如果我们直接使用QT的启动画面类来处理的话,有时候我们遇到电脑显示屏幕不一样的时候,显示的效果并不是特别好,因此,这里我们重新对封装了启动画面类,使用起来也是十分简单,具体过程见如下。

一、显示效果

二、关键程序

1.QtExScreen类的使用

类源文件的使用过程,这些就是全部的过程

#include "QtExScreen.h"

QtExScreen::QtExScreen(const QPixmap& pixmap, QString describe)
	: QWidget(nullptr, Qt::SplashScreen | Qt::FramelessWindowHint)
	, mPixmap(pixmap)
{
	setStyleSheet("QtExScreen{background:white;}");

	int nOffset = 30;
	QString strStyle = "color:white;font: 25 14pt \"微软雅黑 Light\";";
	 
	// 获取屏幕大小
	qreal desktopWidth = QApplication::desktop()->width();
	qreal desktopHeight = QApplication::desktop()->height();

	// 图片缩小
	mPixmap = mPixmap.scaled(desktopWidth, desktopHeight * 0.85, Qt::KeepAspectRatio);

	// 创建启动画面对象
	mSplash = new QSplashScreen(this, mPixmap);
	mSplash->setWindowFlags(Qt::Widget);
	mSplash->setGeometry(mSplash->x(), 0, mSplash->width(), mSplash->height());

	// 设置底部区域
	QWidget* bottomWidget = new QWidget(this);

	// 修饰下样式
	bottomWidget->setStyleSheet(".QWidget{background:#2F4F4F;border:none;}");
	qreal bottomX = mSplash->x();
	qreal bottomY = mSplash->height() - 1;
	qreal bottomWidth = mSplash->width();
	qreal bottomHeight = QApplication::desktop()->height() - bottomY;
	bottomWidget->setGeometry(bottomX, bottomY, bottomWidth, bottomHeight);

	// 添加进度条控件
	mStartProgress = new QProgressBar(bottomWidget);
	mStartProgress->setTextVisible(false);
	mStartProgress->setGeometry(30, bottomWidget->height() / 2, mSplash->width() * 0.5, 30);

	// 添加描述
	QLabel* mMessageLabel = new QLabel(describe, bottomWidget);
	mMessageLabel->setStyleSheet(strStyle);
	mMessageLabel->setText(describe);
	mMessageLabel->setGeometry(mStartProgress->x(), mStartProgress->y() - 30, mStartProgress->width(), 30);
}

QtExScreen::~QtExScreen()
{
}

void QtExScreen::paintEvent(QPaintEvent* event)
{
	return QWidget::paintEvent(event);
}

void QtExScreen::setValue(int value)
{
	if (this->isHidden())
	{
		this->showFullScreen();
	}

	// 设置百分比
	if (value >= 100)
	{
		mStartProgress->setValue(value);
		this->close();
		this->deleteLater();
	}
	else
	{
		mStartProgress->setValue(value);
	}
}

void QtExScreen::setText(const QString& text)
{
	mMessageLabel->setText(text);
}

void QtExScreen::setPixmap(const QPixmap& pixmap)
{
	mPixmap = pixmap;
}

2.main函数中使用

main函数调用的过程如下:

#include "QScreens.h"
#include "QtExScreen.h"
#include
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QScreens w;

	// 启动画面,初始化图片和文字描述
	QtExScreen* splash = new QtExScreen(QPixmap(":/Image/image02.png"),QObject::tr(u8"软件正在加载中...."));
	
	// 设置进度条的值得
	splash->setValue(20);

    // 这里是测试等待的时间,真正使用的时候,可不要真的用延时来阻塞啊
	QThread::msleep(50000);
	
    w.show();
 // 主界面显示出来之后,就直接设置100的进度,然后启动画面就自动关闭了
	splash->setValue(100);
    return a.exec();
}


三、程序链接

https://download.csdn.net/download/u013083044/86403998
QT-好看的启动画面使用_第2张图片

你可能感兴趣的:(Qt,qt,开发语言)