初创品牌网站建设,丰台体育馆网站建设,深夜适合男人看的软件,去哪里找人做网站实现软件启动界面#xff0c;用到QSplashScreen类。
效果 启动界面 描述
QSplashScreen小部件提供了一个可以在应用程序启动期间显示的启动画面。 启动画面通常是在应用程序启动时显示的小部件。启动画面通常用于启动时间较长的应用程序#xff08;例如需要花费一些时间来建…实现软件启动界面用到QSplashScreen类。
效果 启动界面 描述
QSplashScreen小部件提供了一个可以在应用程序启动期间显示的启动画面。 启动画面通常是在应用程序启动时显示的小部件。启动画面通常用于启动时间较长的应用程序例如需要花费一些时间来建立连接的数据库或网络应用程序以向用户提供应用程序正在加载的反馈信息。
启动画面会出现在屏幕的中央。如果希望将启动画面保持在所有其他窗口之上在启动画面的窗口标志中添加Qt::WindowStaysOnTopHint可能会很有用。
一些X11窗口管理器不支持窗口保持在顶部标志。解决方法是设置一个定时器定期对启动画面调用raise()函数以模拟窗口保持在顶部的效果。
最常见的用法是在显示主窗口之前显示启动画面。 下面的代码片段演示了此用法其中显示了一个启动画面并在显示应用程序的主窗口前执行一些初始化任务 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 QSScreen(pixmap);
splash-show();... // 加载一些项
splash-showMessage(已加载模块);qApp-processEvents();... // 建立连接
splash-showMessage(正在建立连接);qApp-processEvents();QSplashScreen支持使用showMessage()函数进行这样的操作。如果希望进行自定义绘制可以使用pixmap()函数获取启动画面使用的位图的指针。或者可以子类化QSplashScreen并重新实现drawContents()函数。
常用函数 构造函数 QSplashScreen(const QPixmap pixmap QPixmap(), Qt::WindowFlags f Qt::WindowFlags())使用给定的位图和窗口标志构造一个QSplashScreen对象。 显示和隐藏 void show()显示启动画面。void finish(QWidget *mainWindow)隐藏启动画面并将其关联到指定的主窗口。在调用此函数之前通常会在显示主窗口前执行一些初始化任务。void clearMessage()清除启动画面上显示的消息。 位图相关 void setPixmap(const QPixmap pixmap)设置启动画面使用的位图。const QPixmap *pixmap() const返回启动画面使用的位图的指针。 消息显示 void showMessage(const QString message, int alignment Qt::AlignBottom | Qt::AlignCenter, const QColor color Qt::black)在启动画面上显示消息。可以设置消息的对齐方式和颜色。 其他 void raise()将启动画面置于顶层。bool isFullScreen() const判断启动画面是否为全屏模式。void setEnabled(bool enabled)设置启动画面是否可用。void setAutoFillBackground(bool enabled)设置是否自动填充启动画面的背景。
子类化示例
.h
#ifndef SCREEN_H
#define SCREEN_H#include QWidget
#include QSplashScreennamespace Ui {
class C_Screen;
}class C_Screen : public QSplashScreen
{Q_OBJECTpublic:explicit C_Screen(QWidget *parent nullptr);~C_Screen();private:Ui::C_Screen *ui;
};#endif // SCREEN_H.cpp
#include Screen.h
#include ui_Screen.h#include QPixmap
#include QFontC_Screen::C_Screen(QWidget *parent) :QSplashScreen(parent),ui(new Ui::C_Screen)
{ui-setupUi(this);QString strScreen :/images/screen1.png;QFont font this-font();font.setPixelSize(23);setFont(font);QPixmap loadingPix(strScreen);setPixmap(loadingPix);}C_Screen::~C_Screen()
{delete ui;
}
在.main调用 C_Screen screen;screen.show();screen.showMessage(程序正在加载......, Qt::AlignTop|Qt::AlignRight, Qt::red);QDateTime time QDateTime::currentDateTime();QDateTime currentTime QDateTime::currentDateTime(); //记录当前时间// 可以添加处理业务while (time.msecsTo(currentTime) 1000) //1000为需要延时的毫秒数{currentTime QDateTime::currentDateTime();}for(int i 0; i 3; i){screen.showMessage(QString(请稍等%1......).arg(3-i), Qt::AlignTop|Qt::AlignRight, Qt::red);time currentTime;while (time.msecsTo(currentTime) 1000) //1000为需要延时的毫秒数{currentTime QDateTime::currentDateTime();}}