[SOLVED] Return value method



  • Hello everyone, I created a class called SearchRequest to connect with a network and return something.

    In the Mainwindow.cpp i created a slot for a PushButton:

    @
    void MainWindow::on_pushButton_clicked()
    {

    SearchRequest *NSearchRequest = new SearchRequest();
    

    }
    @

    And in the SearchRequest class:

    @
    SearchRequest::SearchRequest(QObject *parent) : QObject(parent)
    {

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    
    QUrl url("url here...");
    
    QNetworkRequest request(url);
    
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    QUrl params;
    
    params.addQueryItem("param1", "...");
    
    params.addQueryItem("param2", "...");
    
    params.addQueryItem("param3", "...");
    
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(rfinished(QNetworkReply *)));
    
    QNetworkReply *reply = manager->post(request, params.encodedQuery());
    

    }

    void SearchRequest::rfinished(QNetworkReply *reply)
    {

    QByteArray data = reply->readAll();
    
    QString str(data);
    
    qDebug() << str;
    

    }
    @

    I want to know, how i put the return of the QNetworkReply in one Plain Text Edit that is "ui - design", example:

    @ui->plainTextEdit->appendPlainText(str);@

    str is return of QNetworkReply



  • I'd recommend using signals and slots.
    Tutorial "here":http://doc.qt.digia.com/qt/signalsandslots.html .

    emit a signal in rfinished with QString as a parameter.
    Then receive it in mainwindow slot method, from where you then output in text edit.
    When you create an object SearchRequest, don't forget to connect it.



  • THANK YOU VERY MUCH!



  • If you say you'll emit QString *, you need to emit QString, not QByteArray.

    This should work:
    @emit test(new QString(data));@

    EDIT:
    I swear I saw a problem in post before this ;) .

    Please mark thread as solved ( prepend [SOLVED] in front of a title ( edit first post)).

    Regards,
    Jake



  • Yes, my mistake using * (pointer) like parameter in the signal.

    I just changed to this:

    @
    SearchRequest::SearchRequest(QObject *parent) : QObject(parent)
    {

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    
    QUrl url("url here...");
    
    QNetworkRequest request(url);
    
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    QUrl params;
    
    params.addQueryItem("param1", "...");
    
    params.addQueryItem("param2", "...");
    
    params.addQueryItem("param3", "...");
    
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(rfinished(QNetworkReply *)));
    
    QNetworkReply *reply = manager->post(request, params.encodedQuery());
    

    }

    void SearchRequest::rfinished(QNetworkReply *reply)
    {

    QByteArray data = reply->readAll();
    
    QString str(data);
    
    emit test(data);
    

    }

    signals:

    void test(QString text);

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.