Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4023054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:33:06+00:00 2026-05-20T10:33:06+00:00

I’m having trouble collecting the response from a web request i do. (Because i’m

  • 0

I’m having trouble collecting the response from a web request i do. (Because i’m new to Qt).

Why do i have trouble?

I have a request class which send a request and receive a response. But i can’t get the response to the parent of the request object, because i have to wait for a “finished” signal from the NetworkAccessMaanager which handles the response.

So i handle the response in a “finished” slot, but i can’t return the info to the parent main window holding the request object. How can i do this?

Here’s the code:

Main window.cpp:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_buttonLogin_clicked()
{
    request->sendRequest("www.request.com");
}

Request.cpp:

Request::Request()
{
  oNetworkAccessManager = new QNetworkAccessManager(this);
  QObject::connect(oNetworkAccessManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
}

/*
 * Sends a request
 */
QNetworkReply* Request::sendRequest(QString url)
{
    QUrl httpRequest(url);
    QNetworkRequest request;
    request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // Set default ssl config
    request.setUrl(httpRequest); // Set the url
    QNetworkReply *reply = oNetworkAccessManager->get(QNetworkRequest(httpRequest));

    return reply;
}

/*
 * Runs when the request is finished and has received a response
 */
void Request::finishedSlot(QNetworkReply *reply)
{
       // Reading attributes of the reply
       // e.g. the HTTP status code
       QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
       // Or the target URL if it was a redirect:
       QVariant redirectionTargetUrl =
       reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
       // see CS001432 on how to handle this

       // no error received?
       if (reply->error() == QNetworkReply::NoError)
       {
           // Reading the data from the response
           QByteArray bytes = reply->readAll();
           QString jsonString(bytes); // string

           bool ok;
           QVariantMap jsonResult = Json::parse(jsonString,ok).toMap();
           if(!ok)
           {
               qFatal("An error occured during parsing");
               exit(1);
           }


           // Set the jsonResult
           setJsonResult(jsonResult);


       }
       // Some http error received
       else
       {
           // handle errors here
       }

       // We receive ownership of the reply object
       // and therefore need to handle deletion.
       delete reply;
}

/*
 * Set the json result so that other functions can get it
 */
void Request::setJsonResult(QVariantMap jsonResult)
{
    m_jsonResult = jsonResult;
}

/*
 * Get the json result
 * Return null if there is no result
 */
QVariantMap Request::getJsonResult()
{
    return m_jsonResult;
}

Any ideas of how i can do this?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:33:07+00:00Added an answer on May 20, 2026 at 10:33 am

    Each QNetworkReply emits finished() signal, so you should connect signal from QNetworkReply* returned by request->sendRequest("www.request.com"); to slot of MainWindow.

    EXAMPLE:

    void MainWindow::on_buttonLogin_clicked()
    {
        QNetworkReply *reply = request->sendRequest("www.request.com");
        connect(reply, SIGNAL(finished()), this, SLOT(newslot()));
    }
    
    void MainWindow::newslot()
    {
        QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
        // there you can handle reply as you wish
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.