1 、Qt软件启动界面进度条制作
卓面端的启动界面都用到 QSplashScreen 类 QSplashScreen 类没有启动进度条进行显示,很不美观,如果要进行进度条的加载,则需要对 QSplashScreen 进行继承改造。 继承后、主要要添加 QProgressBar 进度条这个控件进行设计与样式改造
2、具体封装的类如下
class SplashScreen : public QSplashScreen { Q_OBJECT public: explicit SplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()); ~SplashScreen(); void getNumbersList(); void setPixmap(const QPixmap &pixmap); static bool updateProgress(); static void setProgressBarVisible(bool isVisible); static bool m_showProgressBar; static QProgressBar *m_progressBar; static QLabel *m_versionLabel; static QLabel *m_copyrightLabel; static QList<int> m_numbersList; static int m_index; static QString m_progressBarQSS; };
3、代码实现
SplashScreen::SplashScreen(const QPixmap &pixmap, Qt::WindowFlags f) { m_index = 0; m_progressBarQSS = "QProgressBar {border: none; background: transparent;}" "QProgressBar::chunk {background: #FFFFFF;" "border-top-left-radius: 0px;" "border-top-right-radius: %1px;" "border-bottom-right-radius: %2px;" "border-bottom-left-radius: %3px;}" ; getNumbersList(); } SplashScreen::~SplashScreen () { if (m_progressBar) { delete m_progressBar; m_progressBar = Q_NULLPTR; } if (m_versionLabel) { delete m_versionLabel; m_versionLabel = Q_NULLPTR; } if (m_copyrightLabel) { delete m_copyrightLabel; m_copyrightLabel = Q_NULLPTR; } } void SplashScreen::getNumbersList () { qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); for (int i = 0; i <= 20; i++) { m_numbersList.append(i * 5 + qrand() % 5); } m_numbersList[0] = 5; m_numbersList[m_numbersList.count() - 1] = 100; } void SplashScreen::setPixmap(const QPixmap &pixmap) { if (m_showProgressBar) { qreal rate = UnitUtil::dpiScale96(); int height = 8 * rate; qreal pr = UnitUtil::pixelRatio(); m_progressBar = new QProgressBar(this); m_progressBar->setTextVisible(false ); m_progressBar->setGeometry(0, pixmap.height() / pr - height, pixmap.width() / pr, height); m_progressBar->setRange(0, 100); m_progressBar->setValue(0); if (height % 2 == 1) { height--; } int radius = height / 2; m_progressBar->setStyleSheet(m_progressBarQSS.arg(0).arg(0).arg(radius - 1 * rate)); m_progressBarQSS = m_progressBarQSS.arg(0).arg(radius - 1 * rate).arg(radius - 1 * rate); QString labelQSS = "QLabel{background: transparent; color: #FFFFFF; border: none;}" ; m_versionLabel = new QLabel(this); m_versionLabel->setStyleSheet(labelQSS); m_versionLabel->setFont(font); m_versionLabel->setText(tr("Version: " ) + EDVERSION); m_copyrightLabel = new QLabel(this); m_copyrightLabel->setStyleSheet(labelQSS); m_copyrightLabel->setFont(font); m_copyrightLabel->setText(QString("<font>%1</font>" ).arg(tr("Copyright © %1 EdrawSoft. All rights reserved." ).arg(QDate::currentDate().year()))); m_versionLabel->move(72 * rate, 232 * rate); m_copyrightLabel->move(72 * rate, 256 * rate); } QSplashScreen::setPixmap(pixmap); } bool SplashScreen::updateProgress () { if (!m_showProgressBar) return false ; if (m_index >= 0 && m_index < m_numbersList.count()) { if (m_progressBar) { if (m_numbersList[m_index] == 100) { m_progressBar->setStyleSheet(m_progressBarQSS); } m_progressBar->setValue(m_numbersList[m_index]); m_progressBar->repaint(); m_index++; return true ; } else { if (m_progressBar) { m_progressBar->setStyleSheet(m_progressBarQSS); } } return false ; } void SplashScreen::setProgressBarVisible(bool isVisible) { m_showProgressBar = isVisible; if (!m_showProgressBar) { if (m_progressBar) { delete m_progressBar; m_progressBar = Q_NULLPTR; } if (m_versionLabel) { delete m_versionLabel; m_versionLabel = Q_NULLPTR; } if (m_copyrightLabel) { delete m_copyrightLabel; m_copyrightLabel = Q_NULLPTR; } } }
4、
Qpixmap 的主要作用是设置启动界面的的加载图片或背景。启动界面的窗口大小也是根据PixMap的大小进行设定。 updateProgress()这和个函数主要进度条的进度情况。因为是个静态函数,可以在软件进行初始化的地方调用。软件启动每初始化一个地方就调用一下 updateProgress() 更新进度。