Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QObject::connect: Cannot connect (null) problem in QML binding with C++
Forum Updated to NodeBB v4.3 + New Features

QObject::connect: Cannot connect (null) problem in QML binding with C++

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 11.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xiazhouquan
    wrote on last edited by
    #1

    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!

    业精于勤荒于嬉,行成于思毁于随

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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()));
      @

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xiazhouquan
        wrote on last edited by
        #3

        Thank Tobias Hunger ,I have solved the problem.preply= pmanager->get(QNetworkRequest(url)); is ok

        业精于勤荒于嬉,行成于思毁于随

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved