PDA

View Full Version : Problem with QHttp / HTML source download



hojoff79
20th January 2011, 01:14
I am trying to download html source code from a website given in a line edit bar. The code for the function which does so appears below. Everything seems to work fine except i get an error "incomplete type 'QUrl' used in nested name specifier. Which I think is kind of weird since I copied this example directly out of the source documentation.



void MainWindow::gethtml(){
QString address = ui->lineEdit->text();
ui->textEdit->setText(address);
http = new QHttp;
QHttpRequestHeader header("GET", QUrl::toPercentEncoding("/index.html"));
header.setValue("Host", "qt.nokia.com");
http->setHost("qt.nokia.com");
http->request(header);

}


Thank you in advance!

hojoff79
20th January 2011, 22:22
I am now getting an error from the header file on the same code.
There errors are:

In function '~QHttpRequestHeader':
undefined reference to '_imp__ZTV18QHttpRequestHeader' qhttp.h 148
undefined reference to '_imp__ZN11QHttpHeaderD2Ev' qhttp.h 148

When I click on the function and go to that line, the line is just a "{" bracket. So why is it doing this?

Zlatomir
20th January 2011, 22:27
That classes are part of QtNetwork module, do you have:

QT += network
in your .pro file?
If not add it then Rebuild (maybe you will need Clear All too)

hojoff79
20th January 2011, 23:58
That did work, but I am having trouble dealing with the responses, since I'm not very familiar with the QHttp. I tried to set up a request in one function, then I set up a slot in the and it is supposed to run the second function listed below.

I guess I'm not sure how to see errors or successes so I know what is going on with the QHttp request. Also, I don't think I set up the slot properly, the program compiles fine but then it says:

Object::connect: No such signal QHttp::readyRead(http) in ..\htmldownload\mainwindow.cpp:12
Object::connect: (receiver name: 'MainWindow')


The request function


void MainWindow::gethtml(){
QString address = ui->lineEdit->text();

http = new QHttp;
QHttpRequestHeader header("GET", QUrl::toPercentEncoding("/index.html"));
header.setValue("Host", "qt.nokia.com");
http->setHost("qt.nokia.com");
http->request(header);


}


The Slot


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
http = new QHttp;
connect(http, SIGNAL(readyRead(http)), this, SLOT(readthetext()));
}


Code I'm hoping will run when the readReady signal is activated


void MainWindow::readthetext(){
ui->textEdit->setText("IT'S READY!");
QString downloadedtext = http->readAll();
ui->textEdit->setText(downloadedtext);
}


Thank you in advance

Sven
21st January 2011, 08:20
void readyRead ( const QHttpResponseHeader & resp )

Thats the signal. (You pass a QHttp object to that signal, that's why it does not work)

This signal is useful if you want to process the data in chunks as soon as it becomes available. If you are only interested in the complete data, just connect to the requestFinished() signal and read the data then instead. I think this is the better way for your needs.

But anyways you need to pass the "header" to your signal.

This would be the correct solution:

http://www.paste-code.com/paste.php?id=y8gPp5xaRS

( I haven't tested it but i guess it works. Anyways I would prefer the QNetworkAccessManager class to do that stuff. There are some examples how to use it in QtDemo)