Hi,
I am writing a small application to see how QNetworkAccessManager works. I
have taken most of the code from the Qt Http example. On running the code in
qtcreator it seems to hang atthe following line
reply = qnam.get(QNetworkRequest(url));
Is there any additional initialization it have to do to get the
QNetworkAccessManager running on windows destop using qtcreator? I am using
qt 2010.04
Regards,
John
#include "mainwindow.h"
#include "ui_authenticationdialog.h"
HttpWindow::HttpWindow()
{
urlLineEdit = new QLineEdit("http://qt.nokia.com");
urlLabel = new QLabel(tr("&URL:"));
urlLabel->setBuddy(urlLineEdit);
statusLabel = new QLabel(tr("Please enter the URL of a file you want to
download"));
quitButton = new QPushButton(tr("Quit")) ;
downloadButton = new QPushButton(tr("Download"));
downloadButton->setDefault(true);
progressDialog = new QProgressDialog(this);
buttonBox= new QDialogButtonBox;
buttonBox->addButton(downloadButton,QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton,QDialogButtonBox::RejectRole);
connect(urlLineEdit,SIGNAL(textChanged(QString)),this,SLOT(enableDownloadButton())
);
connect(&qnam,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),this,
SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
connect (downloadButton,SIGNAL(clicked()),this, SLOT(downloadFile()));
connect(progressDialog,SIGNAL(canceled()),this, SLOT(cancelDownload()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(urlLabel);
topLayout->addWidget(urlLineEdit);
QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("HTTP"));
urlLineEdit->setFocus();
}
void HttpWindow::enableDownloadButton()
{
downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
}
void HttpWindow::cancelDownload()
{
statusLabel->setText(tr("Download canceled."));
httpRequestAborted = true;
reply->abort();
downloadButton->setEnabled(true);
}
void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator
*authenticator)
{
QDialog dlg;
Ui::Dialog ui;
ui.setupUi(&dlg);
dlg.adjustSize();
ui.siteDescription->setText(tr("%1 at
%2").arg(authenticator->realm()).arg(url.host()));
}
void HttpWindow::downloadFile()
{
url = urlLineEdit->text();
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
if (fileName.isEmpty())
fileName="index.html";
if (QFile::exists(fileName)){
if(QMessageBox::question(this,tr("HTTP"),tr("There already exists a
file called %1 in "
"the current directory.
Overwrite?").arg(fileName),QMessageBox::Yes|QMessageBox::No,QMessageBox::No) ==
QMessageBox::No)
return;
QFile::remove(fileName);
}
file = new QFile(fileName);
if(!file->open(QIODevice::WriteOnly)){
QMessageBox::information(this,tr("HTTP"),tr("nable to save the file %1:
%2.").arg(fileName).arg(file->errorString()));
delete file;
return;
}
progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
downloadButton->setEnabled(false);
startRequest(url);
}
void HttpWindow:: startRequest(QUrl url)
{
reply = qnam.get(QNetworkRequest(url));
connect(reply,SIGNAL(finished()),this,SLOT(httpFinished()));
connect(reply,SIGNAL(readyRead()),this,SLOT(httpReadyRead()));
connect (reply,SIGNAL(downloadProgress(qint64,qint64)),this ,
SLOT(updateDataReadProgress(qint64,qint64)));
}
void HttpWindow::httpReadyRead()
{
// this slot gets called everytime the QNetworkReply has new data.
// We read all of its new data and write it into the file.
// That way we use less RAM than when reading it at the finished()
// signal of the QNetworkReply
if (file)
file->write(reply->readAll());
}
void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(bytesRead);
}
void HttpWindow::httpFinished()
{
progressDialog->hide();
file->flush();
file->close();
}
_______________________________________________
Qt-creator mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-creator