Results 1 to 2 of 2

Thread: connect() return value

  1. #1
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default connect() return value

    The downloadfile() function below connects a finished() reply my function writefile(). I want to have the downloadfile() return an int if it completes correctly, including the writefile(). But I don't know how to have the writefile() return an int to my downloadfile() or the function calling my download file, but I don't know how to put that int into a variable, since it is called through a connect() function and not explicitly within a function. How can I get the return from the writefile() to be part either the downloadfile() or the function that calls the downloadfile().

    Here is the code:
    Qt Code:
    1. int MainWindow::downloadfile(std::string address, std::string filename){
    2. /*takes address from the lineedit, puts it in url, sends request and connects
    3.   Puts the address in class variable address, then links signal readyread
    4.   writetofile, then writefile takes class variable
    5.   */
    6. filenamehold = filename.c_str();
    7. QString tempurl = address.c_str();
    8. QUrl url(tempurl);
    9. reply = manager.get(QNetworkRequest(url));
    10. connect(reply, SIGNAL(finished()), this, SLOT(writefile()));
    11. ui->label_5->setText("Downloading...");
    12. }
    13.  
    14. //function to write into the file after the finished slot is called
    15. int MainWindow::writefile(){
    16. //opens the file and checks to see that it is open, then writes
    17. //info into the file and sets label to done.
    18. file = new QFile(filenamehold);
    19. if (file->open(QIODevice::WriteOnly)){
    20. file->write(reply->readAll());
    21. ui->label_5->setText("Complete!");
    22. return 1;
    23. }
    24.  
    25. //checks for errors and display's if they exist
    26. if (reply->error()){
    27. ui->label_5->setText(reply->errorString());
    28. }
    29.  
    30. return 0;
    31. file->close();
    32. reply->deleteLater();
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: connect() return value

    Wrong thinking!
    Why you need this value in MainWindow::downloadfile?
    Why you cant just process result of MainWindow::writefile in this method? This is still the same class!
    Maybe you should think about emitting a signal in MainWindow::writefile with processing result.

Similar Threads

  1. Replies: 16
    Last Post: 16th February 2010, 13:17
  2. Slot with a return value?
    By lalesculiviu in forum Qt Programming
    Replies: 9
    Last Post: 21st December 2009, 06:27
  3. qdbus connect return always FALSE
    By ciberkids in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2009, 14:17
  4. Replies: 4
    Last Post: 10th November 2006, 15:38
  5. Map as return Type
    By Kapil in forum General Programming
    Replies: 8
    Last Post: 25th May 2006, 20:49

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.