Use different instances with qobject



  • Hi i am trying to use different instances using the follow code:

    QObject::connect(&Downloader::instance(),&Downloader::testSignal,{ });
    

    But show me the follow error:
    error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char ,Qt::ConnectionType) const' : el argumento 2 no puede convertirse de 'void (__thiscall Downloader:: )(void)' a 'const char *'
    conversion not possible

    But this happen because i'm not using lambda thing so i cant call different instances.


  • Moderators

    Hi! Hard to tell, but looks like some problem with a static QObject to me.


  • Lifetime Qt Champion

    Hi,

    What is instance() supposed to return ?



  • @SGaist it's only that i can't use differente instance i need to use "" lambda thing.


  • Lifetime Qt Champion

    You're using a singleton which goal is to have only one instance.

    If you want several instances of your downloader then don't use a singleton.



  • This post is deleted!


  • @SGaist Ok i understand but i have one question:
    I need to use qobject but without singletone to use my other class. sorry if i repeat the code but it's other question with the same code. The question if i can use class outside with qobject and download some files of my webpage without use one only instance. Not necessary solution only some idea thx.
    Code:
    Main.cpp:

    #include <QCoreApplication>
    #include <QDebug>
    #include "downloader.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QObject::connect(&Downloader::instance(),&Downloader::testSignal,
                            [](){
             QString s="http://myweb/currxml.php";
    
           });
        return a.exec();
    }
    

    download.h:

    #ifndef Downloader_H
    #define Downloader_H
    
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QUrl>
    #include <QDateTime>
    #include <QFile>
    #include <QDebug>
    class Downloader : public QObject
    {
        Q_OBJECT
        Q_DISABLE_COPY(Downloader)
    public:
        static Downloader &instance();
        explicit Downloader(QObject *parent = nullptr);
        QString s;
        //virtual ~Downloader(){}
    
        void doSomething();
    signals:
    
        void testSignal();
    
    public slots:
        //void testSlot();
        void replyFinished (QNetworkReply *reply);
    
    private:
        QNetworkAccessManager *manager;
    
    };
    
    #endif // Downloader_H
    

    downloader.cpp

    #include "downloader.h"
    #include <QDebug>
    
    
    Downloader &Downloader::instance()
    {
        static Downloader _instance;
        return _instance;
    }
    
    Downloader::Downloader(QObject *parent) : QObject(parent)
    {
        doSomething();
    }
    
    void Downloader::doSomething()
    {
       //emit testSignal();
        qDebug() << "It's working!!!!";
        manager = new QNetworkAccessManager(this);
        manager->get(QNetworkRequest(QUrl(s)));
        emit instance().testSignal();
    }
    
    void Downloader::replyFinished (QNetworkReply *reply)
    {
        if(reply->error())
        {
            qDebug() << "ERROR!";
            qDebug() << reply->errorString();
        }
        else
        {
            //qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
            //qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
            //qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
            //qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            //qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
            QString p = reply->request().url().fileName();
            QFile *file = new QFile("C:/Users/moh/"+p);
            if(file->open(QFile::Append))
            {
                file->write(reply->readAll());
                file->flush();
                file->close();
            }
            delete file;
        }
    
        reply->deleteLater();
    }
    

  • Lifetime Qt Champion

    You won't see the signal because it's going to be emitted before the event loop is event started.

    What do you mean "use class outside with qobject" ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.