qt中使用啟動畫面

來源:互聯網
上載者:User

                    在程式開啟的時候,啟動畫面是很正常的。

                    對於這個qt提供了QSplashScreen類,可是我在使用過程中,他總是一閃而過,不是我們想要的。我們使用啟動畫面,如果沒有模組檢測,那我們只是想它顯示幾秒鐘而已。下面是我的辦法,繼承QSplashScreen,在加個定時器就行了。

#ifndef SPLASHSCREEN_H

#define SPLASHSCREEN_H

#include <QSplashScreen>
#include <QTimer>

class splashScreen : public QSplashScreen
{
    Q_OBJECT
public:
    explicit splashScreen(const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
   // splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
    void setShowSecond(int _second);//設定啟動畫面的顯現時間長度,單位是秒,不是微秒
    void startTimer()  {show();timer.start();}//定時器開始工作
signals:
    void timeOver();//達到規定的顯示時間長度發出此訊號
public slots:
protected slots:
    void stopTimer();//關閉定時器
private:
    ~splashScreen(){}
    QTimer timer;//顯示時間長度定時器
};

#endif // SPLASHSCREEN_H
 
#include "splashscreen.h"
splashScreen::splashScreen(const QPixmap &pixmap, Qt::WindowFlags f) :
    QSplashScreen(pixmap,f)
{
}
//splashScreen::splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0):
//    QSplashScreen(parent, pixmap, f)
//{
//}
void splashScreen::setShowSecond(int _second)
{
    timer.setInterval(_second*1000);
    connect(&timer,SIGNAL(timeout()),this,SLOT(stopTimer()));
}
void splashScreen::stopTimer()
{
    timer.stop();
    emit timeOver();
    close();
    deleteLater();//此處是特意添加,只能在一次使用,且只能是在heap區使用
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pix(":/picture/splashScreen.jpg");
    splashScreen *ps = new splashScreen(pix);
    ps->setShowSecond(3);
    ps->startTimer();
    MainWindow w;
    QObject::connect(ps,SIGNAL(timeOver()),&w,SLOT(show()));
    w.resize(QApplication::desktop()->size()*0.9);//以裝置的顯示器件的大小來確定主介面的大小
    return a.exec();
}
為什麼我想在啟動畫面結束之後之後直接釋放,因為它就是這時候有用,我不想在程式運行結束的時候在釋放,資源是寶貴的。還有就是預設的滑鼠點擊操作是hide()。如果你不想不小心點了滑鼠畫面消失,並且主介面還沒有出來,還是重寫void mousePressEvent(QMousePressEvent *),函數體為空白就行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.