QSplashScreen Class
QSplashScreen widget 可在应用程序启动时显示闪屏。更多
Header: | #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 |
详细说明
闪屏是一种窗口小部件,通常在应用程序启动时显示。闪屏通常用于启动时间较长的应用程序(如需要一定时间建立连接的数据库或网络应用程序),以便向用户提供应用程序正在加载的反馈信息。
闪屏显示在屏幕中央。如果想让闪屏部件位于桌面上所有其他窗口的上方,可将Qt::WindowStaysOnTopHint 添加到闪屏部件的窗口标志中。
有些 X11 窗口管理器不支持 "保持在顶部 "标记。解决方法是设置一个定时器,定期在闪屏上调用raise() 来模拟 "停留在顶部 "的效果。
最常见的用法是在屏幕上显示主窗口部件之前显示闪屏。下面的代码片段对此进行了说明,在显示应用程序的主窗口之前,会显示一个闪屏并执行一些初始化任务:
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(); }
用户可以通过鼠标点击来隐藏闪屏。为使鼠标处理正常工作,请在启动过程中定期调用QApplication::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();
成员函数文档
[explicit]
QSplashScreen::QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
构建一个闪屏,显示pixmap 。
除了Qt::WindowStaysOnTopHint 之外,应该不需要设置 widget 标志,f 。
QSplashScreen::QSplashScreen(QScreen *screen, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())
这是一个重载函数。
该函数允许您指定闪屏的屏幕。该构造函数的典型用途是,如果您有多个屏幕,并且希望在不同于主屏幕的屏幕上显示闪屏。在这种情况下,请传递适当的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).
void QSplashScreen::finish(QWidget *mainWin)
使闪屏等待直到显示小部件mainWin 后才调用close() 。
QString QSplashScreen::message() const
返回当前显示在闪屏上的信息。
另请参阅 showMessage() 和clearMessage()。
[signal]
void QSplashScreen::messageChanged(const QString &message)
该信号在闪屏上的信息发生变化时发出。message 是新信息,当信息被删除时则为空字符串。
另请参阅 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()。
[slot]
void QSplashScreen::showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black)
将message 文本绘制到闪屏上,颜色为color ,并根据alignment 中的标志对齐文本。该函数调用repaint() 以确保立即重新绘制闪屏。因此,信息将与应用程序正在进行的操作(如加载文件)保持同步。
另请参阅 Qt::Alignment 、clearMessage() 和message()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.