Results 1 to 8 of 8

Thread: Eventloop does not quit properly

  1. #1
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5

    Default Eventloop does not quit properly

    Hello guys,

    i have a question relating QEventloop class. During my initialisation routine some functions should be started sequentially. To gaurantee that i wrote this peace of code:

    Qt Code:
    1. //mainwindow.cpp
    2.  
    3. void MainWindow::doInit()
    4. {
    5. connect(this, SIGNAL(Function1Complete()), &loop, SLOT(quit()));
    6.  
    7. qDebug() << "Step1";
    8. StartFuntion1();
    9. loop.exec();
    10. qDebug() << "Step2";
    11. StartFuntion2();
    12. }
    13.  
    14. void MainWindow::StartFuntion1()
    15. {
    16. ...
    17. emit Function1Complete();
    18. }
    19.  
    20. //mainwindow.h
    21. ...
    22. signals:
    23. void Function1Complete();
    To copy to clipboard, switch view to plain text mode 

    But when i run my code "Step2" is never reached. When i emit "Function1Complete" with an additional button like this
    Qt Code:
    1. void MainWindow::handle_btnTest()
    2. {
    3. emit Function1Complete();
    4. }
    To copy to clipboard, switch view to plain text mode 
    my eventloop quits properly and everything works fine. Any ideas about that?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Eventloop does not quit properly

    First of all You are emmiting signal Function1Complete() before starting internal event loop. In default mode emit signal to object in this same thread
    is equivalent to direct call slot method . So QEventLoop::quit is called before QEventLoop::exec.
    Second in this example StartFunction2() is started sequentially after StartFunction1(). What is real problem ?

  3. #3
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5

    Default Re: Eventloop does not quit properly

    First of all You are emmiting signal Function1Complete() before starting internal event loop. In default mode emit signal to object in this same thread
    is equivalent to direct call slot method . So QEventLoop::quit is called before QEventLoop::exec.
    I just followed this example:
    http://stackoverflow.com/questions/3...nous-qt-signal
    As far as i know "StartFuntion1" will never be reached when you call it after QEventLoop::exec, because the quit condition is called within "StartFuntion1".

    Second in this example StartFunction2() is started sequentially after StartFunction1(). What is real problem ?
    I have the same issue which is described in the url above. StartFuntion1 is an asychronous call.

    StartFunction1 downlaod several *.csv files and StartFunction2 should analyze them. The problem is that StartFunction2 starts to read the files before they are completely downloaded. Sorry, i should mentioned it earlier.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Eventloop does not quit properly

    Show real StartFunction1.

  5. #5
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5

    Default Re: Eventloop does not quit properly

    Show real StartFunction1.
    Sure.

    Qt Code:
    1. void MainWindow::StartFunction1()
    2. {
    3. //Actualize
    4. m_dataPath.setFilter(QDir::Files);
    5. m_dataPath.setNameFilters(QStringList() << "*.csv");
    6. QString strID;
    7. DownloaderDLL *d = new DownloaderDLL;
    8.  
    9. foreach (QString filename, m_dataPath.entryList())
    10. {
    11. strID = QFileInfo(filename).baseName();
    12. strID.replace("_",".");
    13.  
    14. d->doDownload(strID);
    15. }
    16. m_DownloadComplete = true;
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void DownloaderDLL::doDownload(QString str_ID)
    2. {
    3. m_strID=str_ID;
    4. m_strValues="nabl1c1p2t1d1";
    5.  
    6. url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + m_strID + "&f=" + m_strValues + "&e=.csv";
    7.  
    8.  
    9. reply = manager.get(QNetworkRequest(url));
    10. connect(reply, SIGNAL(finished()),
    11. this, SLOT(replyFinished()));
    12. }
    13.  
    14. void DownloaderDLL::replyFinished ()
    15. {
    16. if(reply->error())
    17. {
    18. file->remove();
    19. QMessageBox::warning(NULL, tr("Download data"),
    20. tr("Download failed: %1.")
    21. .arg(reply->errorString()));
    22. }
    23. else
    24. {
    25. QByteArray strContent;
    26. strContent = reply->readAll();
    27. if (strContent.contains("N/A"))
    28. {
    29. QMessageBox::warning(NULL, tr("Download data"),
    30. tr("Could not find ID: %1. \nPlease try again")
    31. .arg(m_strID));
    32. }
    33. else
    34. {
    35. m_strID.replace(".","_");
    36. file = new QFile(QDir::currentPath() + "\\data\\watchlist\\stocks\\" + m_strID + ".csv");
    37.  
    38.  
    39. if(file->open(QFile::Append))
    40. {
    41. file->write(strContent);
    42. QMessageBox::information(NULL, tr("Download data"),
    43. tr("Download finished to following path: %1.")
    44. .arg(file->fileName()));
    45.  
    46. file->flush();
    47. file->close();
    48. }
    49.  
    50.  
    51. }
    52. }
    53.  
    54. reply->deleteLater();
    55. reply = 0;
    56. delete file;
    57. file = 0;
    58. }
    To copy to clipboard, switch view to plain text mode 

    StartFunction1 scans for *.csv files with special ID filenames. This IDs can be used to determine and extract stock values from yahoo. I append actual data to the *.csv files. In the next step i want to scan all *.csv files and extract actual data to show them in a gui. The problem is that the second scan is proceeded before downloading is finished.

    Maybe i could implement the second step within "replyFinished" for each value avoid that problem.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Eventloop does not quit properly

    In the function StartFunction1 you initiate the download of data but nowhere waiting for its completion.

  7. #7
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5

    Default Re: Eventloop does not quit properly

    How i can wait for its completion?

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Eventloop does not quit properly

    Instead of immediately emitting the signal notifying that Function1 has finished, do it in replyFinished() when the last reply has finished.

Similar Threads

  1. crash on qt eventloop
    By waiter in forum Newbie
    Replies: 1
    Last Post: 28th April 2013, 11:28
  2. Replies: 2
    Last Post: 1st August 2011, 06:30
  3. QTcpSocket EventLoop thread is still running?
    By mihau in forum Qt Programming
    Replies: 0
    Last Post: 28th February 2011, 09:41
  4. Problem with eventloop in QThread
    By speedy_gonzales in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 19:17
  5. Thread eventLoop and run
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:36

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.