我有一个代码可以从网上下载一个包。我希望这样的代码在下载完成后运行一个html5viewer(或一个窗口,它会是相同的),这意味着我必须处理完成的()信号,这是我的代码:
的main.cpp
#include <QApplication>
#include "html5applicationviewer.h"
#include "networkmanager.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
NetworkManager manager;
//manager.setFile("http://listadomedicamentos.aemps.gob.es/prescripcion.zip");
manager.setFile("http://listadomedicamentos.aemps.gob.es/prescripcion.zip");
/*
Html5ApplicationViewer viewer;
viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
viewer.showMaximized();
viewer.loadFile(QLatin1String("html/index.html"));*/
return app.exec();
}
networkmanager.h
#include <QObject>
#include "html5applicationviewer.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QStringList>
class NetworkManager : public QObject
{
Q_OBJECT
public:
explicit NetworkManager(QObject *parent = 0);
virtual ~NetworkManager();
void setFile(QString fileURL);
private:
QNetworkAccessManager *manager;
QNetworkReply *reply;
QFile *file;
private slots:
void onDownloadProgress(qint64, qint64);
void onFinished(QNetworkReply *reply);
void onReadyRead();
void onReplyFinished();
};
networkmanager.cpp
#include "networkmanager.h"
#include "html5applicationviewer.h"
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
NetworkManager::NetworkManager(QObject *parent) :
QObject(parent)
{
manager = new QNetworkAccessManager;
}
NetworkManager::~NetworkManager()
{
manager->deleteLater();
}
void NetworkManager::setFile(QString fileURL)
{
QString filePath = fileURL;
QString saveFilePath;
QString savePath;
QStringList filePathList = filePath.split('/');
QString fileName = filePathList.at(filePathList.count() - 1);
savePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
if (QDir(savePath).exists())
{
qDebug() << "Archivos locales para almacenar la base de datos existentes.";
}
else
{
qDebug() << "Creando los archivos locales para almacenar la base de datos...";
QDir().mkdir(savePath);
}
saveFilePath = QString(savePath + "/" + fileName );
QNetworkRequest request;
request.setUrl(QUrl(fileURL));
reply = manager->get(request);
file = new QFile;
file->setFileName(saveFilePath);
file->open(QIODevice::WriteOnly);
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this,
SLOT(onDownloadProgress(qint64,qint64)));
connect(manager, SIGNAL(finished(QNetworkReply*)), this,
SLOT(onFinished(QNetworkReply*)));
connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
}
void NetworkManager::onDownloadProgress(qint64 bytesRead, qint64 bytesTotal)
{
qDebug() << QString::number(bytesRead).toLatin1() + " bytes descargados de " +
QString::number(bytesTotal).toLatin1() + " bytes totales";
}
void NetworkManager::onFinished(QNetworkReply *reply)
{
switch (reply->error())
{
case QNetworkReply::NoError:
{
qDebug() << "El archivo se ha descargado con éxito.";
}
break;
default:
qDebug() << reply->errorString().toLatin1();
}
if (file->isOpen())
{
file->close();
file->deleteLater();
}
}
void NetworkManager::onReadyRead()
{
file->write(reply->readAll());
}
void NetworkManager::onReplyFinished()
{
if (file->isOpen())
{
file->close();
file->deleteLater();
}
}
我尝试过做
connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished(Html5ApplicationViewer&)));
void NetworkManager::onReplyFinished(Html5ApplicationViewer &viewer)
{
viewer.show();
if (file->isOpen())
{
file->close();
file->deleteLater();
}
}
但它会告诉我:
QObject::connect: Incompatible sender/receiver arguments
QNetworkReplyHttpImpl::finished() --> NetworkManager::startViewer(Html5ApplicationViewer&)
如何使这个查看器或窗口以完成的()信号开始?
答案 0 :(得分:1)
您面临的问题是, QNetworkAccessManager的已完成信号在您将其与 Html5ApplicationViewer 作为参数的插槽进行连接时没有任何参数。
这是不兼容的,因此你会收到警告。
如果您想要访问 Html5ApplicationViewer 指针,则需要在 NetworkManager 类中分配或创建指针。