QSplashScreen 小部件提供可以在应用程序启动期间展示的闪屏。 更多...
头: | #include <QSplashScreen> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
继承: | QWidget |
QSplashScreen (const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()) | |
QSplashScreen (QScreen * screen , 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 到 Splash 小部件的窗口标志,若希望使其保持在桌面所有其它窗口之上。
Some X11 window managers do not support the "stays on top" flag. A solution is to set up a timer that periodically calls raise () on the splash screen to simulate the "stays on top" effect.
The most common usage is to show a splash screen before the main widget is displayed on the screen. This is illustrated in the following code snippet in which a splash screen is displayed and some initialization tasks are performed before the application's main window is shown:
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 () 去接收鼠标点击。
It is sometimes useful to update the splash screen with messages, for example, announcing connections established or modules loaded as the application starts up:
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 () function. If you wish to do your own drawing you can get a pointer to the pixmap used in the splash screen with pixmap (). Alternatively, you can subclass QSplashScreen and reimplement drawContents ().
In case of having multiple screens, it is also possible to show the splash screen on a different screen than the primary one. For example:
QScreen *screen = QGuiApplication::screens().at(1); QPixmap pixmap(":/splash.png"); QSplashScreen splash(screen, pixmap); splash.show();
[explicit]
QSplashScreen::
QSplashScreen
(const
QPixmap
&
pixmap
= QPixmap(),
Qt::WindowFlags
f
= Qt::WindowFlags())
构造闪屏显示 pixmap .
应该不需要设置 Widget 标志, f ,或许除了 Qt::WindowStaysOnTopHint .
这是重载函数。
This function allows you to specify the screen for your splashscreen. The typical use for this constructor is if you have multiple screens and prefer to have the splash screen on a different screen than your primary one. In that case pass the proper screen .
[virtual noexcept]
QSplashScreen::
~QSplashScreen
()
析构函数。
[slot]
void
QSplashScreen::
clearMessage
()
移除在闪屏上显示的消息
另请参阅 showMessage ().
[virtual protected]
void
QSplashScreen::
drawContents
(
QPainter
*
painter
)
绘制闪屏的内容,使用描绘器 painter 。默认实现绘制传递的消息,通过 showMessage ()。重实现此函数,若想在闪屏做自己的绘图。
[override virtual protected]
bool
QSplashScreen::
event
(
QEvent
*
e
)
重实现: QWidget::event (QEvent *event).
使闪屏等待,直到小部件 mainWin is displayed before calling close () on itself.
Returns the message that is currently displayed on the splash screen.
另请参阅 showMessage () 和 clearMessage ().
[signal]
void
QSplashScreen::
messageChanged
(const
QString
&
message
)
This signal is emitted when the message on the splash screen changes. message is the new message and is a null-string when the message has been removed.
另请参阅 showMessage () 和 clearMessage ().
[override virtual protected]
void
QSplashScreen::
mousePressEvent
(
QMouseEvent
*)
重实现: QWidget::mousePressEvent (QMouseEvent *event).
Returns the pixmap that is used in the splash screen. The image does not have any of the text drawn by showMessage () 调用。
另请参阅 setPixmap ().
This overrides QWidget::repaint (). It differs from the standard repaint function in that it also calls QCoreApplication::processEvents () to ensure the updates are displayed, even when there is no event loop present.
Sets the pixmap that will be used as the splash screen's image to pixmap .
另请参阅 pixmap ().
[slot]
void
QSplashScreen::
showMessage
(const
QString
&
message
,
int
alignment
= Qt::AlignLeft, const
QColor
&
color
= Qt::black)
绘制 message text onto the splash screen with color color and aligns the text according to the flags in alignment . This function calls repaint () to make sure the splash screen is repainted immediately. As a result the message is kept up to date with what your application is doing (e.g. loading files).
另请参阅 Qt::Alignment , clearMessage (),和 message ().