mydialog.cpp
#include "mydialog.h" //类声明的头文件,需要自己动手写
MyDialog::MyDialog(QWidget *parent)
: QWidget(parent)
{
setupUi(this); //这个非常重要,在构造函数中用这个函数来实现界面,
//没有这个函数的话界面就不会被显示出来
http = new QHttp(this);
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
getWeather();
}
void MyDialog::getWeather() {
QUrl url(QString::fromUtf8("http://www.blogweather.net/MiniWeather.aspx?Area=China&Name=Shenyang&Language=Chinese&Color=Blue"));
fileName = QString::fromUtf8("temp.html");
if (QFile::exists(fileName)) {
return;
}
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());
httpGetId = http->get(url.path(), file);
}
QString MyDialog::getTime() {
return "";
}
void MyDialog::httpRequestFinished(int requestId, bool error)
{
if (requestId != httpGetId)
return;
file->close();
if (error) {
file->remove();
} else {
textBrowser->setSource(fileName);
}
delete file;
file = 0;
}
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QtGui>
#include <QtNetwork/QHttp>
#include "clock.h" //需要从这个文件里的Ui::GoToDialog
class MyDialog : public QWidget, public Ui::Form //多重继承
{
Q_OBJECT
public:
MyDialog(QWidget *parent = 0);
private:
QHttp *http;
QFile *file;
QString fileName;
int httpGetId;
void getWeather();
QString getTime();
private slots:
void httpRequestFinished(int requestId, bool error);
};
#endif