Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to display a splash screen in Qt ?
Forum Updated to NodeBB v4.3 + New Features

How to display a splash screen in Qt ?

Scheduled Pinned Locked Moved General and Desktop
20 Posts 4 Posters 29.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    entricular
    wrote on last edited by
    #11

    Hey KA510, I looked at the code you posted and although it is interesting it is not exactly what I am looking for. I noticed that this code you posted works only on Ubuntu Linux and I was looking for code that worked on both Linux and Windows. So far the closest I have come to what I am looking for is the following code below.

    main.cpp

    @
    #include <QtGui/QApplication>
    #include "splashscreen.h"
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    // lock orientation to portrait when showing the splash screen
    w.lockPortraitOrientation();
    
    // SplashScreen is derived from QSplashScreen that blocks mouse 
    // press events, prevents user from clicking on splashscreen to
    // hide it.
    QPixmap pixmap(":/images/splash.png");
    SplashScreen splash(true, pixmap, Qt::WindowStaysOnTopHint);
    
    // connect loader's progress notifications to splashscreen
    QObject::connect(w.loader(), SIGNAL(progress(QString,int,QColor)), 
                        &splash, SLOT(showMessage(QString,int,QColor)));
    
    
    // on loader done signal, close splashscreen and show mainwindow
    QObject::connect(w.loader(), SIGNAL(done()), &splash, SLOT(close()));
    

    #if defined(Q_WS_S60)
    QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
    #else
    QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
    #endif

    splash.show();
    splash.raise();
    
    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp

    @
    #include "mainwindow.h"
    #include <QtGui/QApplication>
    #include <QMenuBar>
    #include <QWidgetList>

    // needed for S60-specific orientation/softkey handling
    #ifdef Q_WS_S60
    #include <coemain.h>
    #include <aknappui.h>
    #endif

    DummyLoader::DummyLoader(QObject *parent)
    : QObject(parent),
    m_progress(0)
    {
    m_states.insert(15, "Loading resources");
    m_states.insert(30, "Validating");
    m_states.insert(50, "Loading plugins");
    m_states.insert(66, "Initializating");
    m_states.insert(100, "Preparing to launch");

    connect(&m_timer, SIGNAL(timeout()), this, SLOT(doSomething()));
    m_timer.start(500);
    }

    // simulates some activity and fires off notification signals
    void DummyLoader::doSomething()
    {
    m_progress += (qrand() % 5) + 3;
    m_progress = qMin(100, m_progress);

    if ( m_progress == 100 ) {
        m_timer.stop();
        emit done();
        return;
    }
    
    QMap<int, QString>::const_iterator i = m_states.lowerBound(m_progress);
    if ( i != m_states.constEnd() ) {
        QString progressMsg = QString("%1...").arg(i.value());
        emit progress(progressMsg,  Qt::AlignLeft | Qt::AlignTop, Qt::red);
    }
    

    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_orientation(-1)
    {
    menuBar()->addAction(tr("Exit"), this, SLOT(close()));
    removeContextMenus();

    // a dummy progresss notifier as if we were loading something...
    m_loader = new DummyLoader(this);
    

    }

    void MainWindow::removeContextMenus()
    {
    // Remove context menu from the all widgets.
    QWidgetList widgets = QApplication::allWidgets();
    QWidget* w=0;
    foreach(w,widgets) {
    w->setContextMenuPolicy(Qt::NoContextMenu);
    }
    }

    void MainWindow::startUpMaximized()
    {
    // release orientation lock (if set)
    releaseOrientation();

    setWindowFlags(windowFlags() | Qt::WindowSoftkeysVisibleHint);
    showMaximized();
    

    }

    // Lock S60 app orientation to portrait - used when showing the splash screen
    void MainWindow::lockPortraitOrientation()
    {
    #ifdef Q_WS_S60
    CAknAppUi* s60AppUi = dynamic_cast<CAknAppUi*> (CCoeEnv::Static()->AppUi());
    // save the old orientation
    m_orientation = (int)s60AppUi->Orientation();
    TRAP_IGNORE(
    if (s60AppUi) {
    // Lock portrait orientation when showing the splash screen
    s60AppUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
    });
    #endif
    }

    // Releases the orientation lock (restores previous orientation)
    void MainWindow::releaseOrientation()
    {
    // do nothing if orientation is not locked
    if( m_orientation == -1 )
    return;

    #ifdef Q_WS_S60
    CAknAppUi* s60AppUi = dynamic_cast<CAknAppUi*> (CCoeEnv::Static()->AppUi());

    TRAP_IGNORE(
        if (s60AppUi) {
        s60AppUi->SetOrientationL((CAknAppUiBase::TAppUiOrientation)m_orientation);
    });
    

    #endif
    }
    @
    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QTimer>
    #include <QMap>

    class DummyLoader : public QObject
    {
    Q_OBJECT

    public:
    explicit DummyLoader(QObject *parent = 0);

    signals:
    void done();
    void progress(const QString&, int, const QColor&);

    public slots:
    void doSomething();

    private:

    int m_progress;
    QMap<int, QString> m_states;
    QTimer m_timer;
    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget parent = 0);
    ~MainWindow() {}
    const DummyLoader
    loader() const { return m_loader; }

    public slots:
    void startUpMaximized();
    void lockPortraitOrientation();
    void releaseOrientation();

    private:
    void removeContextMenus();

    private:
    int m_orientation;
    DummyLoader* m_loader;
    };

    #endif // MAINWINDOW_H
    @
    splashscreen.h
    @
    #ifndef SPLASHSCREEN_H
    #define SPLASHSCREEN_H

    #include <QSplashScreen>

    class SplashScreen : public QSplashScreen
    {
    Q_OBJECT

    public:
    explicit SplashScreen(bool blocking = false, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0)
    : QSplashScreen(pixmap,f), m_blocking(blocking) {}

    protected: // reimp
    void mousePressEvent ( QMouseEvent *event ) {
    if(!m_blocking)
    QSplashScreen::mousePressEvent(event);
    }

    private:
    bool m_blocking;

    };

    #endif // SPLASHSCREEN_H
    @

    data.qrc
    @

    <RCC>
    <qresource prefix="/">
    <file>images/splash.png</file>
    </qresource>
    </RCC>
    @

    Also need to create a mainwindow.ui file using Qt Designer with dimensions 600x400

    1 Reply Last reply
    0
    • E Offline
      E Offline
      entricular
      wrote on last edited by
      #12

      To run this code see the directions provided below:

      Create a main directory and inside of the directory created an images directory, store your created splash.png image inside of the images directory. Then copy all 5 files into the main directory and use Qt Creator to create a mainwindow.ui file with the dimensions of 600x400. Then run qmake -project, qmake, make. This application runs on Ubuntu Linux and Windows. However, I am trying to figure out how to maximize the main application window to fit the whole computer screen. At this time all it does is show the splashscreen for a few seconds then it kicks it out to a small window/box of the main application.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #13

        [quote]I am trying to figure out how to maximize the main application window to fit the whole computer screen.[/quote]

        Try QWidget::resize(). Or use QWidget::setWindowState() with Qt::WindowMaximized or Qt::WindowFullScreen.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • E Offline
          E Offline
          entricular
          wrote on last edited by
          #14

          Okay I went back and tweaked some of the code, I added the following lines to the main.cpp file

          @
          QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
          @
          It worked it kicked out the main application page to full screen. Look at line #30 in the main.cpp file.

          So far I tested this out on Ubuntu Linux and Windows and it worked perfectly.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            entricular
            wrote on last edited by
            #15

            One issue I can not figure out is why some coders refuse to post the #include directives and the full source code with their shared code, it is always snippets.

            How else is newbie coder going to figure out how to use the shared code in their application? I find this very frustrating.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #16

              It's probably because they want to point out a specific aspect and therefore paste the relevant lines.

              Posting loads of code on a forum isn't exactly what encourages people to post a reply. If you post your complete program and ask "why it isn't working?" it will be pretty much impossible to give an useful answer. If, instead, you only post a small excerpt from your program - the specific part where you have a problem/question - then it will be much easier for other users to understand the issue and to give the desired reply...

              Qt calls its header files exactly like the class they define; shouldn't be too hard to figure out the right include ;)

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #17

                I wonder why you think the code I posted only works on Ubuntu? I wrote this code using visual studio running on a Windows Xp system. I haven't tested it on Linux, but it should run on both platforms.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  entricular
                  wrote on last edited by
                  #18

                  Hello KA51O,

                  You need to add a Qt resource file. In order for your code to display on Windows Vista, 7.

                  This is your code and I included what the Qt Resource file should look like and then you can run. Hopefully, your system is configured to run Qt commands from the command line.

                  Create a directory and pack all your source code and files into this directory
                  The files would consist of main.cpp, splash.png, data.qrc file. Then run the following. Without this data.qrc resource file your splash screen image did not display on my Windows Vista/7 system. However, it did run on Ubuntu Linux without the data.qrc file.

                  qmake -project
                  qmake
                  make

                  This is your code I included the #include directives and added the Qt Resource file known as data.qrc

                  What your code does is display a full page splash screen for 3 seconds. You may want to minimize the full screen splashscreen and have it display for 5 seconds. In my opinion your code needs to be tweaked.

                  main.cpp
                  @
                  #include <QtCore>
                  #include <QApplication>
                  #include <QPixmap>
                  #include <QSplashScreen>
                  #include <QWidget>
                  #include <QMainWindow>
                  #include <QTimer>
                  #include <QThread>
                  #include <QDesktopWidget>
                  #include <QPainter>

                  int main(int argc, char* argv[])
                  {
                  QApplication app(argc, argv);
                  QMainWindow* viewer = new QMainWindow();
                  QImage splashScrImage ("splash.png");
                  QSize screenSize = QApplication::desktop()->geometry().size();
                  QImage splashScr (screenSize, QImage::Format_ARGB32_Premultiplied);
                  QPainter painter (&splashScr);
                  painter.fillRect(splashScr.rect(), Qt::black);
                  QImage scaled = splashScrImage.scaled(screenSize, Qt::KeepAspectRatio);
                  QRect scaledRect = scaled.rect();
                  scaledRect.moveCenter(splashScr.rect().center());
                  painter.drawImage(scaledRect, scaled);
                  QPixmap Logo;
                  Logo.convertFromImage(splashScr);
                  QSplashScreen splashScrWindow (viewer, Logo, Qt::WindowStaysOnTopHint);
                  splashScrWindow.move(0,0);
                  splashScrWindow.show();
                  QTimer::singleShot(3000, &splashScrWindow ,SLOT(close()));
                  viewer->show();
                  return app.exec();
                  }
                  @

                  data.qrc
                  @

                  <RCC>
                  <qresource prefix="/">
                  <file>splash.png</file>
                  </qresource>
                  </RCC>
                  @

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on last edited by
                    #19

                    It was just a code snippet and not supposed to run out of the box. You may be suprised to find out that you're the only one who has the expectation to receive fully compilable code in forum threads.

                    BTW if the file "splash.png" is located in the same folder as the executable, the code will work without a resource file on any platform.

                    Anyways thanks for the effort.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      entricular
                      wrote on last edited by
                      #20

                      Hello, KA51O and MuldeR

                      This is a link to the splash page code I was modifying, here you will find a sample application created using the display splash page method. See if you can compile this and run it on your computer and tell me what you think about it.

                      http://entricularresearchprojects.comyr.com/myprojects.htm

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved