PDA

View Full Version : Update GUI after gettings network result from second class.



Abdeljalil
18th October 2014, 15:04
i have Widget class and another class called QHashCracker.
Hash cracking class uses QNetworkAccessManager to get a result from the internet in Json format and parse it and set the result to class member called reply (of type QString)
HashCracker have a slot called
replyFinishied(QNetworkReply*) that is connected to
finished(QNetworkReply*) signal from QNetworkAccessManager.
and a downloadData() function that connects signals and slots and set QNetworkRequest to the manager.
now after i created a QHashCracker object in the Widget class and run downloadData() in a QPushButton click slot.
this should download the data, parse Json and set the value i need to "reply" member. but after that i want to set the result to QLineEdit in the ui.
here is the problem
network operations needs some time, i tried QTest::qSleep(2000) to pause program execution after downloadData() runs, then show the result in QLineEdit

some code:

downloadData function in QHashCracker class:


void QHashCracker::downloadData()
{
manager = new QNetworkAccessManager(this);
QObject::connect(manager,SIGNAL(finished(QNetworkR eply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl(this->request)));
}



replyFinished slot in QHashCracker class:


void QHashCracker::replyFinished(QNetworkReply *reply)
{
//qDebug() << reply->readAll();
QJsonDocument replyDocument = QJsonDocument::fromJson(reply->readAll());
QJsonObject DocumentObj = replyDocument.object();
QJsonValue ParsingValue = DocumentObj.value("parsed");
this->reply = ParsingValue.toString();
qDebug() << this->reply;
}


on_crack_pushButton_clicked() function in Widget class:


void Widget::on_crack_pushButton_clicked()
{
hashCracker = new QHashCracker(ui->md5crack_lineEdit->text());
hashCracker->downloadData();
QTest::qSleep(1000);
ui->md5cracked_lineEdit->setText(hashCracker->getReply());
}



i hope this is clear.

wysota
19th October 2014, 10:32
When you modify the reply variable, emit a custom signal from the object (e.g. replyChanged) and then you will be able to connect to that signal with setText slot of the line edit.

ChrisW67
19th October 2014, 10:58
Have QHashCracker emit a signal like this:


void resultReceived(const QString &result);

when the result is received. Connect it to the QLineEdit::setText() slot of the line edit. You then do not need to worry about when the request finishes.

You do, however, have to worry about whether the result was successful and various memory leaks.