Results 1 to 4 of 4

Thread: One function in connect() and QTimer::singleShot()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2021
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: One function in connect() and QTimer::singleShot()

    Thank you for all the info.

    App will be running on fairly old PC and there is some other stuff that needs to be processed and loaded (from DB). I was forced to install x64 OS, to install QT on it and is not as fast as i would like (PC and python version App). I did search for lighter versions of Linux, because i don't need anything extra on that PC.

    Search table is not first thing user will see, when MainWindow is visible (i'm using stacked widget). This and OS/App speed was my main reason why i'm using QTimer. To let other stuff get processed and loaded before.

    QTimer with 8500 might be premature at this point, so i might change it later. I started working on C++ version 5 days ago, so i have plenty of time to change QTimer etc.

    I will be using this for now. But as i said, i might change it later.
    Qt Code:
    1. // LOAD TABLES
    2. QTimer::singleShot( 8500, this, SLOT( doInitialSearch() ) );
    3.  
    4. // etc..
    5. }
    6.  
    7. void MainWindow::doInitialSearch()
    8. {
    9. searchTable( 50 );
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: One function in connect() and QTimer::singleShot()

    This and OS/App speed was my main reason why i'm using QTimer. To let other stuff get processed and loaded before.
    Think about a design where each of the parts of "other stuff" has the ability to emit a signal when it is finished, so the next piece of "stuff" can start immediately after that. It doesn't have to be an actual Qt signal, just some flag or other indication that can be used to indicate a "finished" state. In your app, you can implement a state machine that starts the next task when each previous task says it is finished.

    For example, create an enum of status flags:

    Qt Code:
    1. enum TaskStateT
    2. {
    3. eInitialState = 0,
    4. eTask1 = 1,
    5. eTask2 = 2,
    6. // ...
    7. eTaskN = N,
    8. eFinalState = N+1
    9. } TaskState;
    10.  
    11. bool MainWindow::updateTaskState( TaskStateT state )
    12. {
    13. bool bFinished = false;
    14. TaskStateT nextState = TaskStateT( state + 1 );
    15. if ( nextState == eFinalState )
    16. bFinished = true;
    17. else
    18. doTask( nextState );
    19.  
    20. if ( bFinished )
    21. searchTable( 50 );
    22. return bFinished;
    23. }
    24.  
    25. void MainWindow::doTask( TaskStateT state )
    26. {
    27. switch( state )
    28. {
    29. case eTask1:
    30. doTask1();
    31. break;
    32.  
    33. case eTask2:
    34. doTask2();
    35. break;
    36.  
    37. // ...
    38. }
    39.  
    40. // Current task is done, update and do the next
    41. updateTaskState( state );
    42. }
    To copy to clipboard, switch view to plain text mode 

    When the MainWindow constructor is exiting, the last thing it does is call updateTaskState( eInitialState ), which kicks off the set of things that must be done. When all are finished, the initial search can start immediately. You don't need to rely on magic timing numbers which might waste the user's time or start something off too early, depending on how much time the startup stuff takes.

    If you want to keep the GUI "alive" while the initial processing is going on, you could a a call to QCoreApplication::processEvents() in the updateTaskState() method. This will run the event loop and ensure all pending events are handled. There are other ways to do it as well, such as making the state update and task execution into a signal / slot based implementation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QTimer::singleShot(0, ... confusion
    By bjoern83 in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2022, 10:30
  2. QTimer::singleShot in while
    By Ema in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2019, 20:45
  3. QTimer::singleShot stop at 12:00AM on second day
    By croliver in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2016, 17:37
  4. Replies: 3
    Last Post: 31st January 2010, 16:56
  5. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02

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.