PDA

View Full Version : QNAM returns an empty string



ayanda83
29th November 2017, 05:08
Hi Guys, the QNAM in the code below returns an empty string regardless of what website I request.
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
nam = new QNetworkAccessManager(this);
}

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

void Widget::on_swipeBtn_clicked()
{
if(ui->urlLE->text().isEmpty())
{
QMessageBox msgBox;
msgBox.setText("URL field cannot be empty");
msgBox.exec();
}
else
{
connect(nam, &QNetworkAccessManager::finished, this, [this](QNetworkReply *reply){
//ui->htmlTB->setText(reply->readAll());
qDebug() << static_cast<QString>(reply->readAll()) <<endl;
});
nam->get(QNetworkRequest(QUrl(ui->urlLE->text().simplified())));
}
}
What could be causing this?

d_stranz
29th November 2017, 17:04
Instead of using a lambda as the slot, try implementing a real slot in your Widget class and connecting to that instead. If that works, then the problem is in the lambda, not the QNAM. If it doesn't work, then the problem could be in the get() call. Try hard-coding a QUrl (using QString::fromUtf8()) instead of getting it from your UI.

ayanda83
30th November 2017, 05:44
I tried all that but nothing seems to work and with regards to hard coding the url, that defeats what I am trying to do and that is the ability to request any page of my choice.

d_stranz
30th November 2017, 16:43
with regards to hard coding the url, that defeats what I am trying to do

What you are trying to do at this point is to get something working. Minimizing the number of variables reduces the number of places where something could be going wrong. By hard-coding a known URL for testing purposes you can confirm (or not) that your get() call works, without the variability added by through retrieving and reformatting a URL via the UI. If it works with a hard-coded URL, then it points to a problem in formatting the string you have retrieved from the user interface.

And as it says in the QUrl docs, QStrings should be converted from utf8 if they are passed as a URL. Your code doesn't seem to be doing that.