Results 1 to 3 of 3

Thread: Update GUI after gettings network result from second class.

  1. #1
    Join Date
    Mar 2014
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Update GUI after gettings network result from second class.

    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
    Qt Code:
    1. replyFinishied(QNetworkReply*)
    To copy to clipboard, switch view to plain text mode 
    that is connected to
    Qt Code:
    1. finished(QNetworkReply*)
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. void QHashCracker::downloadData()
    2. {
    3. manager = new QNetworkAccessManager(this);
    4. QObject::connect(manager,SIGNAL(finished(QNetworkReply*)),
    5. this,SLOT(replyFinished(QNetworkReply*)));
    6. manager->get(QNetworkRequest(QUrl(this->request)));
    7. }
    To copy to clipboard, switch view to plain text mode 


    replyFinished slot in QHashCracker class:
    Qt Code:
    1. void QHashCracker::replyFinished(QNetworkReply *reply)
    2. {
    3. //qDebug() << reply->readAll();
    4. QJsonDocument replyDocument = QJsonDocument::fromJson(reply->readAll());
    5. QJsonObject DocumentObj = replyDocument.object();
    6. QJsonValue ParsingValue = DocumentObj.value("parsed");
    7. this->reply = ParsingValue.toString();
    8. qDebug() << this->reply;
    9. }
    To copy to clipboard, switch view to plain text mode 

    on_crack_pushButton_clicked() function in Widget class:
    Qt Code:
    1. void Widget::on_crack_pushButton_clicked()
    2. {
    3. hashCracker = new QHashCracker(ui->md5crack_lineEdit->text());
    4. hashCracker->downloadData();
    5. QTest::qSleep(1000);
    6. ui->md5cracked_lineEdit->setText(hashCracker->getReply());
    7. }
    To copy to clipboard, switch view to plain text mode 


    i hope this is clear.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Update GUI after gettings network result from second class.

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update GUI after gettings network result from second class.

    Have QHashCracker emit a signal like this:
    Qt Code:
    1. void resultReceived(const QString &result);
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. QFileSystemModel - Incremental update/pre-emptive update
    By johnnyturbo3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd September 2011, 13:56
  2. Replies: 1
    Last Post: 4th February 2011, 09:09
  3. Status update on Network Disconnected
    By kapoorsudhish in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 12th January 2011, 13:16
  4. Replies: 2
    Last Post: 29th September 2010, 17:44
  5. Replies: 2
    Last Post: 16th July 2010, 19:14

Tags for this Thread

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.