Results 1 to 5 of 5

Thread: QHttp Problem

  1. #1
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QHttp Problem

    Hello,

    I want to add checkLatestVersion functionality to my program. My program will connect foo.com/bar.file and get the latestVersion and compares with currentVersion. If latestVersion is greater then currentVersion that it will give a message (there is a new version, download it) and then quit.

    Here is my code:
    Qt Code:
    1. void VersionChecker::checkVersion()
    2. {
    3. versionHttp = new QHttp();
    4. connect(versionHttp,SIGNAL(requestFinished(int,bool)),this,SLOT(versionHttpData(int,bool)));
    5. versionHttp>setHost("www.foo.com", 80);
    6. versionHttpRequestId = versionHttp->get("/bar/latest.txt");
    7. }
    8.  
    9. void VersionChecker::versionHttpData(int id, bool error)
    10. {
    11. if (id != versionHttpRequestId)
    12. {
    13. return;
    14. }
    15. if(error)
    16. {
    17. latestVersionString = "2.0.0.0";
    18. return;
    19. }
    20. latestVersionString = versionHttp->readAll();
    21. }
    22.  
    23. QString VersionChecker::getLatestVersion()
    24. {
    25. return latestVersionString;
    26. }
    To copy to clipboard, switch view to plain text mode 

    and how i use it:

    Qt Code:
    1. QApplication a(argc, argv);
    2.  
    3. VersionChecker *versionControl = new VersionChecker();
    4. versionControl->checkVersion();
    5. QString latestVersion = versionControl->getLatestVersion();
    6.  
    7. if(latestVersion != _PROGRAM_VERSION)
    8. {
    9. NewVersionAvailableDialog *newVersionDialog = new NewVersionAvailableDialog();
    10. newVersionDialog->setModal(true);
    11. newVersionDialog->setAttribute( Qt::WA_DeleteOnClose );
    12. newVersionDialog->show();
    13. return a.exec();
    14. }
    15.  
    16.  
    17. MainForm w;
    18. w.show();
    19. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    20. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    The problem is, the program runs, QHttp connects and gets the latest version but, while these are being MainWindow is shown. I dont want it to be shown. Firstly latestVersion control must be finished. If current version is the latest version, then MainWindow must shown, otherwise i want the program to quit.

    How can I do that? Where's the problem?

  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: QHttp Problem

    QHttp needs time and a running event loop to fetch the results. You can try:
    Qt Code:
    1. versionControl->checkVersion();
    2. QCoreApplication::processEvents();
    3. QString latestVersion = versionControl->getLatestVersion();
    To copy to clipboard, switch view to plain text mode 
    You can also change VersionChecker a bit and make it show either the main window or NewVersionAvailableDialog. In such case your code will look like this:
    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. VersionChecker checker;
    6. checker->checkVersion();
    7.  
    8. return app.exec();
    9. }
    10. ...
    11. void VersionChecker::runningOldVersion()
    12. {
    13. NewVersionAvailableDialog dlg;
    14. dlg.exec();
    15. }
    16. ...
    17. void VersionChecker::runningNewestVersion()
    18. {
    19. MainForm *mf = new MainForm();
    20. mf->setAttribute( Qt::WA_DeleteOnClose );
    21. mf->show();
    22. }
    23. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHttp Problem

    Ok I solved like in your second solution. Thanks for help

  4. #4
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHttp Problem

    But now there exists another problem. I have a firewall. Each time after i compile my program, firewall asks me permission for accessing internet. I say yes. No problem, it works sucessfully. But when i say no, the program still tries to connect to my server. It waits xx seconds (or minutes).

    1- How can I solve it?
    2- Is there a timeout mechanism for QHttp?
    3- What should be if no firewall but slow internet connection?

  5. #5
    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: QHttp Problem

    Quote Originally Posted by musaulker View Post
    2- Is there a timeout mechanism for QHttp?
    Yes, it is, but it's well hidden. You can create an ordinary socket and set a timeout using setsockopt(), next you'll need to wrap it in a QTcpSocket and use QHttp::setSocket().

    It would be nice, if there was some Qt-way to change socket options, but so far I haven't seen any.

    Quote Originally Posted by musaulker View Post
    1- How can I solve it?
    As a workaround you can make your own timeout mechanism using QTimer.

Similar Threads

  1. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02
  2. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.