QSplashScreen 類

QSplashScreen 小部件提供可以展示的閃屏,在應用程序啓動期間。 更多...

頭: #include <QSplashScreen>
qmake: QT += widgets
繼承: QWidget

公共函數

QSplashScreen (QScreen * screen , 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 )

公共槽

void clearMessage ()
void showMessage (const QString & message , int alignment = Qt::AlignLeft, const QColor & color = Qt::black)

信號

void messageChanged (const QString & message )

保護函數

virtual void drawContents (QPainter * painter )

重實現保護函數

virtual bool event (QEvent * e ) override
virtual void mousePressEvent ( QMouseEvent * ) override

詳細描述

閃屏是應用程序啓動時,通常顯示的 Widget。閃屏常用於長啓動時間的應用程序 (如:數據庫或花時間建立連接的網絡應用程序),為用戶提供應用程序加載反饋。

閃屏齣現在屏幕中心。它可能很有用,添加 Qt::WindowStaysOnTopHint 到閃屏 Widget 的窗口標誌,若想要把它保持在桌麵所有其它窗口之上。

一些 X11 窗口管理器不支持 "停留在頂部" 標誌。解決方案是設置計時器周期性調用 raise () 在閃屏以模擬 "停留在頂部" 效果。

最常見用法是展示閃屏,在屏幕中顯示主 Widget 前。以下代碼片段將闡明這,顯示閃屏並履行一些初始化任務,在展示應用程序主窗口前:

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();
}
					

The user can hide the splash screen by clicking on it with the mouse. Since the splash screen is typically displayed before the event loop has started running, it is necessary to periodically call QCoreApplication::processEvents () 以接收鼠標點擊。

有時很有用采用消息更新閃屏,例如,在應用程序啓動時宣布建立連接 (或加載模塊):

QPixmap pixmap(":/splash.png");
QSplashScreen *splash = new QSplashScreen(pixmap);
splash->show();
... // Loading some items
splash->showMessage("Loaded modules");
QCoreApplication::processEvents();
... // Establishing connections
splash->showMessage("Established connections");
QCoreApplication::processEvents();
					

QSplashScreen 支持這,采用 showMessage () 函數。若希望自己繪製,可以獲取用於閃屏的像素圖指針采用 pixmap ()。另外,可以子類 QSplashScreen 並重實現 drawContents ().

若擁有多個屏幕,在不同屏幕而不是首要屏幕,展示閃屏是可能的。例如:

QScreen *screen = QGuiApplication::screens().at(1);
QPixmap pixmap(":/splash.png");
QSplashScreen splash(screen, pixmap);
splash.show();
					

成員函數文檔編製

QSplashScreen:: QSplashScreen ( QScreen * screen , const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())

這是重載函數。

此函數允許為閃屏指定屏幕。此構造函數的典型用法是若擁有多個屏幕,且首選讓閃屏在不同屏幕而不是首要屏幕。在此情況下,傳遞適當 screen .

該函數在 Qt 5.15 引入。

QSplashScreen:: QSplashScreen (const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())

構造閃屏顯示 pixmap .

應該不需要設置 Widget 標誌 f ,或許除瞭 Qt::WindowStaysOnTopHint .

[slot] void QSplashScreen:: clearMessage ()

移除在閃屏上顯示的消息

另請參閱 showMessage ().

[signal] void QSplashScreen:: messageChanged (const QString & message )

此信號發射,當閃屏中的消息改變時。 message 為新消息,和為 null 字符串當消息已移除時。

另請參閱 showMessage () 和 clearMessage ().

[slot] void QSplashScreen:: showMessage (const QString & message , int alignment = Qt::AlignLeft, const QColor & color = Qt::black)

繪製 message 文本在閃屏中采用顔色 color 並對齊文本根據標誌 alignment 。此函數調用 repaint () 以確保立即重新描繪閃屏。因此,消息隨應用程序在做的保持更新 (如:加載文件)。

另請參閱 Qt::Alignment , clearMessage (),和 message ().

[虛擬] QSplashScreen:: ~QSplashScreen ()

析構函數。

[virtual protected] void QSplashScreen:: drawContents ( QPainter * painter )

繪製閃屏的內容,使用描繪器 painter 。默認實現繪製傳遞的消息,通過 showMessage ()。重實現此函數,若想在閃屏做自己的繪圖。

[override virtual protected] bool QSplashScreen:: event ( QEvent * e )

重實現: QWidget::event (QEvent *event).

void QSplashScreen:: finish ( QWidget * mainWin )

使閃屏等待,直到小部件 mainWin 顯示先於調用 close () 在自身。

QString QSplashScreen:: message () const

返迴閃屏中目前顯示的消息。

該函數在 Qt 5.2 引入。

另請參閱 showMessage () 和 clearMessage ().

[override virtual protected] void QSplashScreen:: mousePressEvent ( QMouseEvent * )

重實現: QWidget::mousePressEvent (QMouseEvent *event).

const QPixmap QSplashScreen:: pixmap () const

返迴閃屏中使用的像素圖。圖像不擁有任何繪製文本通過 showMessage () 調用。

另請參閱 setPixmap ().

void QSplashScreen:: repaint ()

這覆寫 QWidget::repaint ()。它不同於標準重新描繪函數,因為它還調用 QCoreApplication::processEvents () 以確保顯示更新,即使不存在事件循環。

void QSplashScreen:: setPixmap (const QPixmap & pixmap )

把用作閃屏圖像的像素圖設為 pixmap .

另請參閱 pixmap ().