iis 添加網(wǎng)站 win7種子搜索神器
目錄
QSplashScreen 類介紹
?使用方式
項(xiàng)目中使用
THPrinterSplashScreen頭文件
?THPrinterSplashScreen實(shí)現(xiàn)代碼
使用代碼?
使用效果
?
QSplashScreen 類介紹
?????????QSplashScreen?是?Qt?中的一個(gè)類,用于顯示啟動(dòng)畫(huà)面。它通常在應(yīng)用程序啟動(dòng)時(shí)顯示,以向用戶顯示應(yīng)用程序正在啟動(dòng)的狀態(tài)。啟動(dòng)畫(huà)面可以是一個(gè)圖片,也可以是一個(gè)包含了文本、圖片等內(nèi)容的窗口。
QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
virtual?~QSplashScreen()
void?finish(QWidget *mainWin)QString?message() const
const QPixmap?pixmap() const
void?repaint()
void? setPixmap(const QPixmap &pixmap)//slots
void?clearMessage()
void?showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black)//protected 可以繼承自繪
virtual void?drawContents(QPainter *painter)
?
?使用方式
????????以下是Qt官方文檔給出的兩種使用場(chǎng)景。
? 作為主窗口啟動(dòng)前的啟動(dòng)動(dòng)畫(huà)
int main(int argc, char *argv[]){QApplication app(argc, argv);QPixmap pixmap(":/splash.png");QSplashScreen splash(pixmap);splash.show();app.processEvents();...QMainWindow window;window.show();splash.finish(&window);return app.exec();}
主窗口啟動(dòng)前軟件啟動(dòng)提示信息
QPixmap pixmap(":/splash.png");QSplashScreen *splash = new QSplashScreen(pixmap);splash->show();... // Loading some itemssplash->showMessage("Loaded modules");qApp->processEvents();... // Establishing connectionssplash->showMessage("Established connections");qApp->processEvents();
項(xiàng)目中使用
? ? ? ? 實(shí)際項(xiàng)目中如果軟件啟動(dòng)比較耗時(shí),一般需要根據(jù)軟件的樣式風(fēng)格和互動(dòng)需求自定義啟動(dòng)動(dòng)畫(huà)效果,此時(shí)virtual void?drawContents(QPainter *painter) 和?repaint()就顯得尤為重要。
????????以下是根據(jù)自身項(xiàng)目,加載啟動(dòng)動(dòng)畫(huà)時(shí)顯示軟件版本信息和啟動(dòng)進(jìn)度等信息,主要繼承drawContents進(jìn)行重繪。
THPrinterSplashScreen頭文件
#ifndef THPrinterSplashScreenT_H
#define THPrinterSplashScreenT_H#include <QSplashScreen>
#include "Common.h"#define g_pSplashScreen Singleton<THPrinterSplashScreen>::getInstance()class THPrinterSplashScreen : public QSplashScreen
{Q_OBJECTfriend Singleton<THPrinterSplashScreen>;
public://關(guān)閉自身前可以再次操作void finish(QWidget *w);//設(shè)置啟動(dòng)進(jìn)度0-100void setProgressValue(int value);//設(shè)置啟動(dòng)提示信息 如庫(kù)加載信息、數(shù)據(jù)庫(kù)啟動(dòng)...void setTipStr(const QString&tipStr);protected://重寫(xiě)此函數(shù) 自定義繪制啟動(dòng)動(dòng)畫(huà)void drawContents(QPainter *painter) override;
private:THPrinterSplashScreen();~THPrinterSplashScreen() = default;QPixmap m_pixIcon;QPixmap m_picBackground;int m_nProgressValue = 0;QString m_strTip;
};#endif // THPrinterSplashScreenT_H
?THPrinterSplashScreen實(shí)現(xiàn)代碼
#pragma execution_character_set("utf-8")THPrinterSplashScreen::THPrinterSplashScreen()
{m_picBackground.load(":/images/icon/background.png");m_pixIcon.load(":/images/icon/logo.png");setPixmap(m_picBackground);setWindowFlag(Qt::WindowStaysOnTopHint);
}void THPrinterSplashScreen::finish(QWidget *w)
{setProgressValue(100);setTipStr("程序加載完成!");QSplashScreen::finish(w);
}void THPrinterSplashScreen::setProgressValue(int value)
{if (isVisible() && value >= 0 && m_nProgressValue < value) {value = qBound(0, value,100);m_nProgressValue = value;repaint();}
}void THPrinterSplashScreen::setTipStr(const QString&tipStr)
{if (isVisible() && !tipStr.isEmpty() && m_strTip != tipStr) {m_strTip = tipStr;repaint();}
}void THPrinterSplashScreen::drawContents(QPainter *painter)
{QSplashScreen::drawContents(painter);int bg_w = m_picBackground.width();int bg_h = m_picBackground.height();int icon_w = m_pixIcon.width();int icon_h = m_pixIcon.height();//默認(rèn)垂直方向dpi為96 防止不同設(shè)備分辨率不同字體差異過(guò)大float fFactor = logicalDpiY() / 96.0f; int smallFontSize = qRound(10 * fFactor);int midFontSize = qRound(15 * fFactor);int bigFontSize = qRound(20 * fFactor);int fontGapSize = 6;int magrinGapSize = 10;int offset = -20;int icon_x = (bg_w - icon_w) / 2;int icon_y = (bg_h - icon_h) / 2 + offset;int text_name_y = (bg_h + icon_h) / 2 + magrinGapSize + offset;int text_TipStr_y = text_name_y + bigFontSize + fontGapSize;int text_version_y = bg_h - fontGapSize - midFontSize;QRect rect_Icon(icon_x, icon_y, icon_w, icon_h);//相對(duì)于parent 左上角坐標(biāo) 長(zhǎng)寬QRect rect_Name_Text(0, text_name_y, bg_w, bigFontSize + fontGapSize);QRect rect_TipStr_Text(0, text_TipStr_y, bg_w, smallFontSize + fontGapSize);QRect rect_Version_Text(0, text_version_y, bg_w, midFontSize + fontGapSize);// 繪制啟動(dòng)動(dòng)畫(huà)logopainter->drawPixmap(rect_Icon, m_pixIcon);//繪制軟件名稱auto font = painter->font();font.setBold(true);font.setPointSize(bigFontSize);painter->setFont(font);auto pen = painter->pen();pen.setColor(Qt::white);painter->setPen(pen);painter->drawText(rect_Name_Text, Qt::AlignCenter, tr("設(shè)備指紋燒錄工具"));//繪制啟動(dòng)中提示信息font = painter->font();font.setBold(false);font.setPointSize(smallFontSize);painter->setFont(font);if (!m_strTip.isEmpty()){painter->drawText(rect_TipStr_Text, Qt::AlignCenter, m_strTip);}//繪制軟件版本信息font = painter->font();font.setPointSize(midFontSize);painter->setFont(font);auto &strVersion = PmsUpDater::getVersion();if (!strVersion.isEmpty()) {painter->drawText(rect_Version_Text, Qt::AlignCenter, strVersion);}//在rect_Version_Text最右側(cè)繪制軟件啟動(dòng)進(jìn)度if (m_nProgressValue >= 0) {rect_Version_Text.adjust(0, 0, -midFontSize, 0);painter->drawText(rect_Version_Text,Qt::AlignVCenter | Qt::AlignRight,QString("%1%").arg(m_nProgressValue));}
}
使用代碼?
main函數(shù)中嵌入到軟件主界面啟動(dòng)前后。
int main(int argc, char *argv[]){//...g_pSplashScreen->setProgressValue(0);g_pSplashScreen->show();PmsUpDater w;w.show();g_pSplashScreen->finish(&w);//...return a.exec();}
????????在程序啟動(dòng)比較耗時(shí)的地方添加進(jìn)度信息和提示信息,便于判斷程序啟動(dòng)的狀態(tài),若程序啟動(dòng)失敗也可作為定位失敗位置的信息。
int THPrinter::Initial(){//...//初始化SDKInitialSdk();g_pSplashScreen->setTipStr("SDK初始化成功!");g_pSplashScreen->setProgressValue(53);//...//數(shù)據(jù)庫(kù)連接開(kāi)始g_pSplashScreen->setTipStr("數(shù)據(jù)庫(kù)連接中...");g_pSplashScreen->setProgressValue(56);//...//連接完成g_pSplashScreen->setTipStr("數(shù)據(jù)庫(kù)連完成");g_pSplashScreen->setProgressValue(57);//...}