Results 1 to 7 of 7

Thread: Allowing external events to fire during a tight loop

  1. #1
    Join Date
    May 2006
    Posts
    58
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Allowing external events to fire during a tight loop

    In my application I have run into a situation (SFTP downloading) where I need to repeatedly loop for quite a bit of time inside of an event. As of right now, my whole app is frozen during the download, which is of course not desirable.

    My first instinct is to use a QThread for the download, but that would require a fair amount of re-coding of the existing app. Is there a way I could hand back control to the app's event pump during my loop? I was thinking of something like this:

    Qt Code:
    1. //Called from my button click slot
    2. int DownloadFile() {
    3. if( m_Semaphore ) {
    4. return(0);
    5. }
    6. m_Semaphore = 1; //To keep this function from being called again during download
    7. int bytesleft = checkfilesize();
    8. while( bytesleft ) {
    9. bytesleft -= downloadsomedata();
    10. //Is there something I could call like this that allows app
    11. //Event loop to continue?
    12. ::AllowMessagePumpToRun();
    13. }
    14.  
    15. m_Semaphore = 0;
    16. } //DownloadFile
    To copy to clipboard, switch view to plain text mode 

    I know this isn't the world's most desirable solution, but I was hoping that I could accomplish this without a major rewrite of this code. The SFTP overhead I have in my class is somewhat bulky, and I doin't want to have to move all of this over into a new class. (I know, it was a bad idea to tie all of this functionality into my Window class)

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Allowing external events to fire during a tight loop

    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    hardgeus (27th December 2006)

  4. #3
    Join Date
    May 2006
    Posts
    58
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Allowing external events to fire during a tight loop

    Quote Originally Posted by jpn View Post
    This works great, but now if the user clicks the "x" button in the top right to shut down the program, the download just continues in the background until it is complete. I already have a boolean that can cancel the download, and I have a CancelDownload slot attached to a Cancel button, which works fine. I tried connecting this slot to the destroyed() signal of the Window to let the loop know it's time to quit, but that event is never fired. I connected to the aboutToQuit() signal of QCoreApplication, which is getting fired under normal circumstances (debug() shows that the signal is firing), but this signal never gets fired as long as I'm in my tight loop.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Allowing external events to fire during a tight loop

    Quote Originally Posted by hardgeus View Post
    I tried connecting this slot to the destroyed() signal of the Window to let the loop know it's time to quit, but that event is never fired.
    This is because the window is not deleted when it's closed. You can get it deleted upon close by setting attribute Qt::WA_DeleteOnClose. See QWidget::setAttribute().
    J-P Nurmi

  6. #5
    Join Date
    May 2006
    Posts
    58
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Allowing external events to fire during a tight loop

    Quote Originally Posted by jpn View Post
    This is because the window is not deleted when it's closed. You can get it deleted upon close by setting attribute Qt::WA_DeleteOnClose. See QWidget::setAttribute().
    I set that, and I'm pretty sure that it is now being deleted, because I got segfaults on close until I realized I had to change to dynamic allocation of my window after setting DeleteOnClose. Still no dice, my slot gets called when the program closed during idle, but it isn't getting called inside of the loop.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Allowing external events to fire during a tight loop

    What if you override QWidget::closeEvent() and set the flag there? The event handler should get called at least.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    hardgeus (28th December 2006)

  9. #7
    Join Date
    May 2006
    Posts
    58
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Allowing external events to fire during a tight loop

    Quote Originally Posted by jpn View Post
    What if you override QWidget::closeEvent() and set the flag there? The event handler should get called at least.
    That works and the upload stops! Praise the gods. For a minute, I was getting a segfault, but it turns out I was popping up a messagebox on cancel which was crashing. Thanks for all of your help!

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04

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
  •  
Qt is a trademark of The Qt Company.