QSplashScreen
简介:Qt5中可以通过QSplashScreen实现程序的启动界面,其会在应用程序的主窗口显示前显示一个图片,在该图片上可以通过文字来显示想要输出的信息。
API
#ifndef QSPLASHSCREEN_H
#define QSPLASHSCREEN_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qpixmap.h>
#include <QtWidgets/qwidget.h>
QT_REQUIRE_CONFIG(splashscreen);
QT_BEGIN_NAMESPACE
class QSplashScreenPrivate;
class Q_WIDGETS_EXPORT QSplashScreen : public QWidget
{
Q_OBJECT
public:
explicit QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags());
QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags());
virtual ~QSplashScreen();
void setPixmap(const QPixmap &pixmap);
const QPixmap pixmap() const;
void finish(QWidget *w);
void repaint();
QString message() const;
public Q_SLOTS:
void showMessage(const QString &message, int alignment = Qt::AlignLeft,
const QColor &color = Qt::black);
void clearMessage();
Q_SIGNALS:
void messageChanged(const QString &message);
protected:
bool event(QEvent *e) override;
virtual void drawContents(QPainter *painter);
void mousePressEvent(QMouseEvent *) override;
private:
Q_DISABLE_COPY(QSplashScreen)
Q_DECLARE_PRIVATE(QSplashScreen)
};
QT_END_NAMESPACE
#endif // QSPLASHSCREEN_H
示例代码
#include "widget.h"
#include <QApplication>
#include <QSplashScreen>
#include <thread>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *spl = new QSplashScreen;
spl->setPixmap(QPixmap(":/test.jpg").scaled(400,400));//设置要显示的图片
Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;//设置位置:右上
spl->showMessage("test", align, Qt::red);//设置图片上文本信息
spl->show();//显示图片
std::this_thread::sleep_for(std::chrono::seconds(2));
Widget w;
spl->finish(&w);//关闭显示
w.show();
delete spl;//释放资源
spl = nullptr;
return a.exec();
}