#include "networkget.h"
NetWorkGet::NetWorkGet(QUrl url, QString filename)
{
file = new QFile("./temp/"+filename);
file->open(QIODevice::WriteOnly|QIODevice::Text);
//获取网络图片
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(url));
}
void NetWorkGet::replyFinished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
file->write(reply->readAll());
file->close();
file->flush();
delete file;
file = 0;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NetWorkGet *n = new NetWorkGet(QUrl("http://api.bilibili.cn/bangumi"),"bangumi");
//MainWindow w; // 这么写槽执行
//w.show();
return a.exec();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
NetWorkGet *n = new NetWorkGet(QUrl("http://api.bilibili.cn/bangumi"),"bangumi");
ui->setupUi(this);//写在mainwindow 槽不执行 不知道怎么回事
}
------解决思路----------------------
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(url));
这2行交换下顺序呢?
------解决思路----------------------
connect成功了吗?运行时没警告?