QObject::connect: Cannot connect (null) problem in QML binding with C++
-
I implement a class to download a file from http
@
DownLoadFile.h#ifndef DOWNLOADFILE_H
#define DOWNLOADFILE_H
#include <QObject>
#include <QUrl>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>class CDownLoadFile: public QObject
{
Q_OBJECT
public:
explicit CDownLoadFile(QObject *parent=0);public slots:
Q_INVOKABLE void downloadFile(QUrl url);
Q_INVOKABLE void httpReadyRead();
Q_INVOKABLE void httpFinished();
private:
QUrl url;
QNetworkAccessManager *pmanager;
QNetworkReply *preply;
QFile *pfile;
};#endif // DOWNLOADFILE_H
@@
DownLoadFile.cpp
#include "DownLoadFile.h"
#include <QtGui>
#include <QtNetwork>
#include <QNetworkAccessManager>CDownLoadFile::CDownLoadFile(QObject *parent)
:QObject(parent)
{}
void CDownLoadFile::downloadFile(QUrl url)
{
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
pfile = new QFile(fileName);
pmanager=new QNetworkAccessManager();
QObject::connect(preply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
QObject::connect(preply,SIGNAL(finished()),this,SLOT(httpFinished()));
preply= pmanager->get(QNetworkRequest(url));
}void CDownLoadFile::httpReadyRead()
{
if (pfile)
pfile->write(preply->readAll());
}void CDownLoadFile::httpFinished()
{
pfile->flush();pfile->close(); preply->deleteLater();
preply = 0;
delete pfile; pfile = 0;
}
@In the QML file ,I use it link this
@
import CDownLoadFile 1.0
CDownLoadFile{id:epubdownload}
epubdownload.downloadFile("http://s3.amazonaws.com/manybooksepub/munroeki3565235652-8epub.epub")@
I register CDownLoadFile in the main.cpp link this
@
qmlRegisterType<CDownLoadFile>("CDownLoadFile", 1,0, "CDownLoadFile");@
But when I run the application,something wrong with this:
QObject::connect: Cannot connect (null)::readyRead() to CDownLoadFile::httpReadyRead()
QObject::connect: Cannot connect (null)::finished() to CDownLoadFile::httpFinished()I can't find any solution about the problem!
Thank you for your reply!
My regards! -
You are connecting the object preply points to (most likely preply is 0 at that point) and then have it point to the object you care about.
Try this instead:
@
preply= pmanager->get(QNetworkRequest(url));
QObject::connect(preply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
QObject::connect(preply,SIGNAL(finished()),this,SLOT(httpFinished()));
@ -
Thank Tobias Hunger ,I have solved the problem.preply= pmanager->get(QNetworkRequest(url)); is ok