PDA

View Full Version : QHttp of QT Assistant example



thomasjoy
2nd August 2007, 15:00
hi all
i am working on MacOS X and using QT4.1 for application

In QHttp Example of Qt Manual when we start download any .exe or .dmg from server then it takes some time span then it show the progressDialog where when basically downloading get started

now My Question is after click the download botton and till then downloading get started what processes are taken place???

and can we show 3-4 label text on the dialog (where the download button is placed)that would be keep on changing till then the downloading get started ???

and if it can be done then how it can be done ????


thanks in advance

marcel
2nd August 2007, 15:24
Well, if you look over the sources, almost everything happens in HttpWindow::downloadFile. This slot gets called when you press the download button.



void HttpWindow::downloadFile()
{
QUrl 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::Ok|QMessageBox::Cancel, QMessageBox::Cancel)
== QMessageBox::Cancel)
return;
QFile::remove(fileName);
}

file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("HTTP"),
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
return;
}

QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());

if (!url.userName().isEmpty())
http->setUser(url.userName(), url.password());

httpRequestAborted = false;
httpGetId = http->get(url.path(), file);

progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
downloadButton->setEnabled(false);
}


It first does some decent verifications(if the file already exists locally, etc).

The delay you mentioned probably comes from QHttp. It is the time it takes QHttp to connect to server and starting downloading the file. I guess that is OK.



and can we show 3-4 label text on the dialog (where the download button is placed)that would be keep on changing till then the downloading get started ???

Well, you can, but I guess you won't have time to show much things in there.

Regards

thomasjoy
2nd August 2007, 15:50
firstly thanks for replying
i know well what is happening in this process but if i wanna show some changing label text , like for two second label text on dialog is one and for another 3,4,5 seconds the label text should be changed, in that time spam which is taken by http

if it is possible the please do write some example code for it???

marcel
2nd August 2007, 16:08
I think you can see when the download actually starts if you hook to the dataReadProgress signal.
When you first receive this signal then it means that the first chunk has been donwloaded.

To display some text I suggest using QTimer.
I cannot give you an example since I am not sure it would work. Just a draft, maybe:


in constructor:
mTimer = new QTimer();
mTimer->setInterval(1000); //1 second;
mStringList << "message1";
mStringList << "message2";
mStringList << "message3";
mStringList << "message4";
connect(mTimer, SIGNAL(timeout()), this, SLOT(cycleMessages()));
connect(downloadButton, SIGNAL(clicked()), mTimer, SLOT(start())); //start the timer when download is pressed

this is the timer slot:
void HttpWindow::cycleMessages()
{
int msgIndex = mStringList.indexOf(firstLabel->text());
if(msgIndex < 0 )
msgIndex = 0;
else
msgIndex++;
if(msgIndex >= mStringList.count())
msgIndex = 0;

firstLabel->setText(mStringList.at(msgIndex);
}

void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes)
{
...
mTimer->stop();
}


So there you will have a label that just displays those 4 messages in a cycle. The message is switched every second.
When it reaches the end of the list, it starts again from the beginning.

mTimer is a QTimer* member.
mStringList is a QStringList member.
firstLabel is a QLabel* member.



Make sure to delete the QTimer in the destructor.

Regards

thomasjoy
3rd August 2007, 07:16
but do tell me it is not be connected as on the download button Http example had put the another signal that is downloadFile() which get started as we click it
so on one button two slot how we can put??