Results 1 to 7 of 7

Thread: QTimer based Splash Screen

  1. #1
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTimer based Splash Screen

    Hey guys,

    I have a dialog widget setup to be a custom splash screen.
    I am overriding paintEvent to draw some boxes on the widget.
    I have a QTimer setup to run a method every half second or so.
    This method updates an array and does a repaint() wich calls
    paintEvent. The code in paintEvent draws the boxes on the dialog
    with different opacities based on values in the array.

    My problem is that my QTimer is not triggering my method.

    Here is my code:
    Qt Code:
    1. CINWaitSplash::CINWaitSplash(QWidget *parent, Qt::WindowFlags f)
    2. : QWidget(parent, f)
    3. {
    4. // Main Event Timer
    5. m_timerMain = new QTimer(this);
    6. connect(m_timerMain, SIGNAL(timeout()), this, SLOT(FillTable_SnakePattern()));
    7. m_timerMain->setInterval(100); // Start our Timer 1000 = 1sec
    8. m_timerMain->setSingleShot(false);
    9. }
    To copy to clipboard, switch view to plain text mode 

    My MainWindow has a routine that calls these to start and stop the timer:
    Qt Code:
    1. void CINWaitSplash::Start()
    2. {
    3. InitTable(); // Start A Blank Table
    4.  
    5. this->show(); // Show Dialog
    6.  
    7. m_timerMain->start();
    8. }
    9.  
    10. void CINWaitSplash::Stop()
    11. {
    12. m_timerMain->stop();
    13.  
    14. this->hide(); // Hide Dialog
    15. }
    To copy to clipboard, switch view to plain text mode 

    So when I call Start() it displays my Dialog Widget and "starts" the timer. My MainWindow sleeps for like 10 seconds then calls Stop(). It is doing that just for testing. Anyways... my FillTable_SnakePattern() method never gets triggered by the timer. At the end of the method I am doing a repaint() like I said earlier. I am going to use this to pop up a slash screen when you do a search in my program. When the search is done, it will call Stop() and hide the splash screen.

    Am I doing something wrong here? I am assuming the timer runs on another thread... right?

    So if I accessed my splashscreen class like this from my mainwindow:

    CINWaitSplash* CINMainWindow::INWaitSplash() { return m_qtINWaitSplash; }

    I would be able to start and stop it like so:

    INMainWindow()->INWaitSplash()->Start();
    // sleep 10 seconds
    INMainWindow()->INWaitSplash()->Stop();

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer based Splash Screen

    Are there any messages on the console? Did you add Q_OBJECT macro and declared FillTable_SnakePattern() as a slot?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer based Splash Screen

    Do you have an event loop running?

  4. #4
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer based Splash Screen

    Header File:
    Qt Code:
    1. #ifndef __INWAITSPLASH_H__
    2. #define __INWAITSPLASH_H__
    3.  
    4. #include "ui_INWaitSplash.h"
    5.  
    6. #define ROWSIZE 4
    7. #define COLUMNSIZE 4
    8. #define TAILLENGTH 4
    9.  
    10. class CINWaitSplash : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. CINWaitSplash(QWidget * parent = 0, Qt::WindowFlags f = Qt::SplashScreen|Qt::WindowStaysOnTopHint);
    16.  
    17. void Init();
    18. void Start();
    19. void Stop();
    20.  
    21. protected:
    22. void paintEvent(QPaintEvent *);
    23. void InitTable();
    24.  
    25. private slots:
    26. // Table Pattern Algorithms
    27. void FillTable_SnakePattern();
    28. //void FillTable_RandomPattern();
    29. //void FillTable_BoxPattern();
    30.  
    31. private:
    32. Ui::SplashForm ui;
    33.  
    34. QTimer* m_timerMain;
    35.  
    36. int m_aiCellTable[COLUMNSIZE][ROWSIZE];
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    As for an event loop... I would have to say no because I am not sure what an event loop is. It is strange because if I turn the timers setInterval(100) to start(100) it will trigger... but after I run my Start() method from a function in my MainWindow it stops... and obviously stops on my Stop() method. So I know the connection is setup. I must be missing something fundamental.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer based Splash Screen

    What do you do after you call Start()? If you don't have an event loop running (either by calling QDialog::exec() or QApplication::exec() or reapeating QApplication:rocessEvents() calls) timers won't work.

  6. #6
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer based Splash Screen

    Interesting...

    This is what I am doing.
    I have a class that contains a pointer to my MainWindow and my Splash Screen Widget.
    There I create a "singleton" instance of it that my class that can be used in my MainWindow's class. So inside a method of my MainWindow I am calling something like this:

    Qt Code:
    1. void CINMainWindow::Search()
    2. {
    3. INMyApp()->INWaitSplash()->Start();
    4. CINTimer::Sleep(1000); // Something meaningful would be done here
    5. INMyApp()->INWaitSplash()->Stop();
    6. }
    To copy to clipboard, switch view to plain text mode 

    So I am calling Start() and Stop() to my splash screen class from any where I want.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer based Splash Screen

    It doesn't have any chance to work then. If you want to use timers, you have to call QApplication::exec() before or right after Start() and do the rest as small steps triggered by another timer with timeout set to 0, more or less like so:
    Qt Code:
    1. void MyClass::init1(){
    2. //...
    3. QTimer::singleShot(0, this, SLOT(init2()));
    4. }
    5.  
    6. void MyClass::init2(){
    7. //...
    8. QTimer::singleShot(0, this, SLOT(init3()));
    9. }
    10.  
    11. // ...
    12.  
    13. QTimer::singleShot(0, &MyObjectOfMyClass, SLOT(init1()));
    14. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Timers can only timeout() when the control is in the event loop, so having a single long operation won't allow them to be triggered as well. You either have to use QApplication:rocessEvents() or use 0-timeout timers like on the example above.

Similar Threads

  1. Problem with Splash Screen ?
    By vinod in forum Qt Programming
    Replies: 13
    Last Post: 11th April 2020, 18:15
  2. Replies: 3
    Last Post: 8th December 2006, 19:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.