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. Question about qobject and http
Forum Updated to NodeBB v4.3 + New Features

Question about qobject and http

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

    Hi i am trying to download files of my webpage with http. But i want send the string since qobject connection or is not possible? The other thing i'm doing with functions but maybe will better or can be do with signal/slots like always.
    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
      #2

      Hi,

      First thing: do not create static QObject.

      Make your _instance a pointer and initialize it the first time instance is called.

      Also, isn't this question the same as the one you posted here ?

      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
      0
      • SGaistS SGaist

        Hi,

        First thing: do not create static QObject.

        Make your _instance a pointer and initialize it the first time instance is called.

        Also, isn't this question the same as the one you posted here ?

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

        @SGaist it's not similar because here i ask about how i can pass since my main function one string to my other class with qobject. in the other i ask about instances i think maybe i'm wrong. But the code is the same.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          First thing: do not create static QObject.

          Make your _instance a pointer and initialize it the first time instance is called.

          Also, isn't this question the same as the one you posted here ?

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

          @SGaist when you say make your instance like pointer can you say some example? Other question about this post, the code is the same but in the post i am asking about other thing . But anyways what you think i need to delete other post?

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

            Here you have an example.

            If you think both subjects are unrelated, then keep both.

            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
            1
            • SGaistS SGaist

              Here you have an example.

              If you think both subjects are unrelated, then keep both.

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

              @SGaist one question this can be a good way?
              class:

              //GlobalClass.h
              
              class GlobalClass
              {
              public:
                static GlobalClass* get()
                {
                  if ( m_instance == nullptr )
                  {
                    m_instance = new GlobalClass;
                  }
                  return m_instance;
                }
                void set_value( int value )
                {
                  m_value = value;
                }
              
                int get_value()
                {
                  return m_value;
                }
              
                ~GlobalClass()
                {
                  delete m_instance;
                }
              
              private:
                GlobalClass() : m_value( 0 )
                {
                }
              
                static GlobalClass* m_instance;
              
                int m_value;
              };
              
              

              And then i can use, using this:

              GlobalClass::get()->set_value(2);
              auto val = GlobalClass::get();
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Yes it's one correct way to implement 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

                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