Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use different instances with qobject
Forum Updated to NodeBB v4.3 + New Features

Use different instances with qobject

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.8k Views 2 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.
  • J Offline
    J Offline
    Jeronimo
    wrote on last edited by Jeronimo
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What is instance() supposed to return ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What is instance() supposed to return ?

          J Offline
          J Offline
          Jeronimo
          wrote on last edited by Jeronimo
          #4

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 2 Replies Last reply
            1
            • SGaistS SGaist

              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.

              J Offline
              J Offline
              Jeronimo
              wrote on last edited by Jeronimo
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                J Offline
                J Offline
                Jeronimo
                wrote on last edited by Jeronimo
                #7

                @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();
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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" ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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