Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Return value method
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Return value method

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beowulf
    wrote on last edited by
    #1

    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

    -- 0x00

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      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.


      Code is poetry

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beowulf
        wrote on last edited by
        #3

        THANK YOU VERY MUCH!

        -- 0x00

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jake007
          wrote on last edited by
          #4

          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


          Code is poetry

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beowulf
            wrote on last edited by
            #5

            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);

            @

            -- 0x00

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved