Qt 啟動畫面

Qt 啟動畫面

參考網址

https://wiki.qt.io/Custom_splashscreen_with_text

執行畫面

流程

準備一張 .png 圖檔匯入專案中
1. 專案右鍵


2. Add New

3. Add Qt Resource File

4. name: png resource

5. 確認新增成功

 
6. 新增資料夾 Add Prefix1

7. 前置字串名稱可以修改

8. 新增 source 資料夾,把圖片放入之後匯入檔案
匯入檔案需要把圖片先放入,否則路徑會錯誤

重點

QSplashScreen 要先確認有支援哪些 API

QElapseedTimer 主要靠 Timer 停留3秒,否則畫面會一閃而過看不到 QSplashScreen,此處可以做一些耗時的動作.

圖檔位置要確認,否則會錯誤

程式碼

main.cpp

<code class="prettyprint">
#include "mainwindow.h"

#include <QApplication>
#include <QElapsedTimer>
#include <QRect>
#include <QSplashScreen>
#include <QTimer>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    customSplashScreen* splash = new customSplashScreen(QPixmap(":/png/source/start_up.png"));
    splash->setMessageRect(QRect(135, 484, 415, 28)/*, Qt::AlignHCenter | Qt::AlignVCenter*/);

    QFont splashFont;
    splashFont.setFamily("Arial");
    splashFont.setBold(true);
    splashFont.setPixelSize(20);
    splashFont.setStretch(125);
    splash->setFont(splashFont);

    splash->show();

    int intdelayTime = 3;

    QElapsedTimer timer;
    timer.start();

    while(timer.elapsed()<(intdelayTime*1000))
    {
        splash->showStatusMessage(QObject::tr("Loading something…"), Qt::black);
        app.processEvents();
    }

    QMainWindow *mainWindow = new QMainWindow;
    mainWindow->show();

    splash->finish(mainWindow);
    delete splash;

    return app.exec();
}
</code>

mainwindow.cpp

<code class="prettyprint">
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
</code>

mainwindow.h

<code class="prettyprint">
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
</code>

customsplashscreen.cpp

<code class="prettyprint">
#include "customsplashscreen.h"

#include <QSplashScreen>
#include <QPainter>

customSplashScreen::customSplashScreen(const QPixmap& pixmap)
{
    QSplashScreen::setPixmap(pixmap);
}

customSplashScreen::~customSplashScreen() {

}

void customSplashScreen::drawContents(QPainter *painter)
{
    QPixmap textPix = QSplashScreen::pixmap();
    painter->setPen(this->color);
    painter->drawText(this->rect, this->alignement, this->message);
}

void customSplashScreen::showStatusMessage(const QString &message, const QColor &color)
{
    this->message = message;
    this->color = color;
    this->showMessage(this->message, this->alignement, this->color);
}

void customSplashScreen::setMessageRect(QRect rect, int alignement)
{
    this->rect = rect;
    this->alignement = alignement;
}
</code>

customsplashscreen.h

<code class="prettyprint">
#ifndef CUSTOMSPLASHSCREEN_H
#define CUSTOMSPLASHSCREEN_H

#include <QSplashScreen>
#include <QPainter>

class customSplashScreen : public QSplashScreen
{
public:
    customSplashScreen(const QPixmap& pixmap);
    ~customSplashScreen();
    virtual void drawContents(QPainter *painter);
    void showStatusMessage(const QString &message, const QColor &color = Qt::black);
    void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);

private:
    QString message;
    int alignement;
    QColor color;
    QRect rect;
};

#endif // CUSTOMSPLASHSCREEN_H
</code>

.pro

<code class="prettyprint">
RESOURCES += \
    png_source.qrc
</code>

留言

熱門文章

TourCard 開通全紀錄 台胞證 支付寶

封神榜國際服 符石系統