QSplashScreen Class :Qt程序啓動畫面類
QSplashScreen類是QT環境提供的程序啓動畫面組件(widget:我喜歡稱組件,當然大家都叫他部件),提供了一個啓動屏幕,可以在應用程序啓動期間顯示它。爲後續的應用程序初始化贏得時間。
目錄
QSplashScreen Class :Qt程序啓動畫面類
Reimplemented Protected Functions:重新實現保護函數:同名同參覆蓋:virtual多態實現。
void QSplashScreen::clearMessage() [slot]
void QSplashScreen::messageChanged(const QString&message) [signal]
QSplashScreen::~QSplashScreen() [virtual]
void QSplashScreen::drawContents(QPainter*painter) [virtual protected]
bool QSplashScreen::event(QEvent *e) [override virtual protected]
void QSplashScreen::finish(QWidget *mainWin)
QString QSplashScreen::message() const
void QSplashScreen::mousePressEvent(QMouseEvent * event) [override virtual protected]
const QPixmap QSplashScreen::pixmap() const
void QSplashScreen::setPixmap(const QPixmap &pixmap)
QSplashScreen類的名片信息:
Header: | #include <QSplashScreen> |
qmake: | QT += widgets |
Inherits: | QWidget |
Public Functions 公共函數:
構造函數 | QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()) |
構造函數 | QSplashScreen(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) |
Public Slots 公共槽函數:
void | clearMessage() |
void | showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black) |
Signals 信號:
void | messageChanged(const QString &message) |
Protected Functions 保護函數:
virtual void | drawContents(QPainter *painter) |
Reimplemented Protected Functions:重新實現保護函數:同名同參覆蓋:virtual多態實現。
virtual bool | event(QEvent *e) override |
virtual void | mousePressEvent(QMouseEvent *) override |
詳細說明:
在程序初始化時,往往我們需要一個啓動畫面,也成爲歡迎畫面、初始畫面。 啓動畫面通常用於啓動時間較長的應用程序(例如需要花費時間來建立連接的數據庫或網絡應用程序),以向用戶提供應用程序正在加載的反饋,以告訴客戶程序正在正常運行,並沒有出現故障。
正常情況下我們會把啓動畫面放在屏幕的正中央。如果我們還想把啓動畫面放在其他所有軟件的最前面,也就是不讓其他軟件遮擋住我們的啓動畫面,我們應該使用Qt :: WindowStaysOnTopHint標誌。實例:
//因爲QSplashScreen類實現的組件本身就是無邊框界面所以我們可以省略Qt::FramelessWindowHint
//this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
//正確的寫法是隻添加Qt::WindowStaysOnTopHint標誌
setWindowFlags(Qt::WindowStaysOnTopHint);
//設置全屏顯示。
this->showFullScreen();
某些X11窗口管理器不支持Qt :: WindowStaysOnTopHint標誌 (停留頂部標誌)。 一種解決方案是設置一個計時器,該計時器在啓動屏幕上定期調用raise()以模擬“停留在頂部”的效果。
X11也叫做X Window系統,X Window系統 (X11或X)是一種 位圖 顯示的 視窗系統 。它是在 Unix 和 類Unix 操作系統 ,以及 OpenVMS 上建立圖形用戶界面 的標準工具包和協議,並可用於幾乎所有已有的現代操作系統。
QSplashScreen類最常見的用法是在主窗口顯示在屏幕上之前顯示初始屏幕。下面的代碼片段說明了這一點,其中顯示了初始屏幕,並在顯示應用程序主窗口之前執行了一些初始化任務:
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();
}
用戶可以用鼠標點擊啓動界面來隱藏初始屏幕。由於啓動屏幕通常在事件循環開始運行之前顯示,因此需要定期調用QCoreApplication::processEvents()以接收鼠標單擊。
有時用消息更新啓動屏幕是很有用的,例如,在應用程序啓動時宣佈連接已建立或模塊已加載:
QPixmap pixmap(":/splash.png");
QSplashScreen *splash = new QSplashScreen(pixmap);
splash->show();
... // Loading some items
splash->showMessage("Loaded modules");
qApp->processEvents();
... // Establishing connections
splash->showMessage("Established connections");
qApp->processEvents();
showMessage效果:
qsprashscreen通過showMessage()函數支持此功能。如果您想繪製自己的圖形,可以使用pixmap()獲得一個指向啓動屏幕中使用的pixmap的指針。或者,可以繼承qsprashscreen類,重新實現drawContents()方法。
成員函數文檔:
QSplashScreen::QSplashScreen(QWidget*parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
這是一個重載函數。
此函數允許您爲splashscreen指定父級。此構造函數的典型用途是,如果您有多個屏幕,並且希望在不同於主屏幕的屏幕上顯示初始屏幕。在這種情況下,傳遞正確的desktop()作爲父級。
QSplashScreen::QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
構造一個初始屏幕,將顯示像素圖。
因爲QsplashScreen爲特別定製的窗口類型,我們一般除了設置f = Qt::WindowFlags()標誌,其他標誌不需要設置。
void QSplashScreen::clearMessage() [slot]
刪除初始屏幕上顯示的消息
與之對應的函數:showMessage()。
void QSplashScreen::messageChanged(const QString&message) [signal]
當初始屏幕上的消息更改時,將發出此信號。 message是新消息,並且在刪除消息後爲空字符串。
與之有關聯的函數:showMessage() 和 clearMessage()。
void QSplashScreen::showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black) [slot]
將消息文本以彩色繪製到初始屏幕上,並根據對齊的標誌對齊文本。此函數調用repaint()以確保立即重新繪製初始屏幕。因此,該消息與應用程序正在執行的操作(例如加載文件)保持最新。
與之有關聯的函數與聲明: Qt::Alignment, clearMessage(), and message()。
Qt::Alignment:
水平標記:
常數 | 值 | 說明 |
---|---|---|
Qt::AlignLeft |
0x0001 |
與左邊緣對齊。 |
Qt::AlignRight |
0x0002 |
與右邊緣對齊。 |
Qt::AlignHCenter |
0x0004 |
在可用空間中水平居中。 |
Qt::AlignJustify |
0x0008 |
對齊可用空間中的文本。 |
垂直標誌:
常數 | 值 | 說明 |
---|---|---|
Qt::AlignTop |
0x0020 |
與頂部對齊。 |
Qt::AlignBottom |
0x0040 |
與底部對齊。 |
Qt::AlignVCenter |
0x0080 |
在可用空間中垂直居中。 |
Qt::AlignBaseline |
0x0100 |
與基線對齊。 |
二維標誌:
常數 | 值 | 說明 |
---|---|---|
Qt::AlignCenter |
AlignVCenter | AlignHCenter |
在兩個維度上均居中。 |
一次最多可以使用一個水平和一個垂直標誌。Qt::AlignCenter計算爲水平和垂直。
三個枚舉值對於可以從右向左模式運行的應用程序非常有用:
常數 | 值 | 說明 |
---|---|---|
Qt::AlignAbsolute |
0x0010 |
如果小部件的佈局方向是Qt::right to left(而不是默認的Qt::left to right),Qt::AlignLeft表示右邊緣,Qt::AlignRight表示左邊緣。這通常是期望的行爲。如果希望Qt::AlignLeft始終表示“left”,Qt::AlignRight始終表示“right”,請將標誌與Qt::AlignAbsolute組合。 |
Qt::AlignLeading |
AlignLeft |
|
Qt::AlignTrailing |
AlignRight |
對齊組合:
常數 | 值 |
---|---|
Qt::AlignHorizontal_Mask |
AlignLeft | AlignRight | AlignHCenter | AlignJustify | AlignAbsolute |
Qt::AlignVertical_Mask |
AlignTop | AlignBottom | AlignVCenter | AlignBaseline |
QSplashScreen::~QSplashScreen() [virtual]
析構函數。
void QSplashScreen::drawContents(QPainter*painter) [virtual protected]
使用Painter 繪製初始屏幕的內容。 默認實現繪製showMessage()傳遞的消息。 如果要在初始屏幕上繪製自己的圖形,請重新實現此功能。
bool QSplashScreen::event(QEvent *e) [override virtual protected]
重新實現event【事件】功能函數。
void QSplashScreen::finish(QWidget *mainWin)
使初始屏幕等待窗口小部件mainWin顯示,然後對其本身調用close()。
QString QSplashScreen::message() const
返回當前在啓動屏幕上顯示的消息。
Qt 5.2中引入了此功能。
void QSplashScreen::mousePressEvent(QMouseEvent * event) [override virtual protected]
重新實現鼠標按下事件。
const QPixmap QSplashScreen::pixmap() const
返回初始屏幕中使用的像素圖。 該圖像不包涵showMessage()調用繪製的任何文本。
與之相關聯的函數: setPixmap()。
void QSplashScreen::repaint()
這將覆蓋QWidget::repaint()。它不同於標準的重繪函數,因爲它還調用QCoreApplication::processEvents()以確保顯示更新,即使不存在事件循環。
void QSplashScreen::setPixmap(const QPixmap &pixmap)
將用作啓動屏幕圖像的像素映射設置爲像素映射。
與之相關聯的函數: pixmap()。
QSplashScreen類在使用中,關係最密切的應該是QPixmap類、QPainter類。