PDA

View Full Version : connect() return value



hojoff79
16th February 2011, 13:27
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:


int MainWindow::downloadfile(std::string address, std::string filename){
/*takes address from the lineedit, puts it in url, sends request and connects
Puts the address in class variable address, then links signal readyread
writetofile, then writefile takes class variable
*/
filenamehold = filename.c_str();
QString tempurl = address.c_str();
QUrl url(tempurl);
reply = manager.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(writefile()));
ui->label_5->setText("Downloading...");
}

//function to write into the file after the finished slot is called
int MainWindow::writefile(){
//opens the file and checks to see that it is open, then writes
//info into the file and sets label to done.
file = new QFile(filenamehold);
if (file->open(QIODevice::WriteOnly)){
file->write(reply->readAll());
ui->label_5->setText("Complete!");
return 1;
}

//checks for errors and display's if they exist
if (reply->error()){
ui->label_5->setText(reply->errorString());
}

return 0;
file->close();
reply->deleteLater();
}

MarekR22
16th February 2011, 14:48
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.