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. My first Qt socket programming program
Forum Updated to NodeBB v4.3 + New Features

My first Qt socket programming program

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 1.7k 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,

    I finally started learning Qt socket programming by this video. It's beneficial but outdated.
    Here is my downloader.h file:

    #ifndef DOWNLOADER_H
    #define DOWNLOADER_H
    
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QFile>
    #include <QDebug>
    
    class Downloader : public QObject
    {
        Q_OBJECT
    public:
        explicit Downloader(QObject *parent = nullptr);
    
        void Do_download();
    
    signals:
    
    public slots:
        
        void stateChanged(int);
        void responseHeaderReceived(const QHttpResponseHeader&);  // problem
        void readyRead(const QHttpResponseHeader&);  // problem 
        void requestFinished(int, bool);
    
    private:
        QNetworkAccessManager* http;
    };
    
    #endif // DOWNLOADER_H
    

    I used QNetworkAccessManager instead of QHttp there, which is in my opinion the Qt 5.12 version of it.

    In minute 5, Bryan uses the following methods for Qt 4 I suppose. My question is what are the equivalents of these two methods in Qt 5.12.1, please?

    void responseHeaderReceived(const QHttpResponseHeader&);
    void readyRead(const QHttpResponseHeader&);
    

    I aim at finishing this task using the current version of Qt.

    J.HilkJ 1 Reply Last reply
    0
    • tomyT tomy

      Hi all,

      I finally started learning Qt socket programming by this video. It's beneficial but outdated.
      Here is my downloader.h file:

      #ifndef DOWNLOADER_H
      #define DOWNLOADER_H
      
      #include <QObject>
      #include <QNetworkAccessManager>
      #include <QFile>
      #include <QDebug>
      
      class Downloader : public QObject
      {
          Q_OBJECT
      public:
          explicit Downloader(QObject *parent = nullptr);
      
          void Do_download();
      
      signals:
      
      public slots:
          
          void stateChanged(int);
          void responseHeaderReceived(const QHttpResponseHeader&);  // problem
          void readyRead(const QHttpResponseHeader&);  // problem 
          void requestFinished(int, bool);
      
      private:
          QNetworkAccessManager* http;
      };
      
      #endif // DOWNLOADER_H
      

      I used QNetworkAccessManager instead of QHttp there, which is in my opinion the Qt 5.12 version of it.

      In minute 5, Bryan uses the following methods for Qt 4 I suppose. My question is what are the equivalents of these two methods in Qt 5.12.1, please?

      void responseHeaderReceived(const QHttpResponseHeader&);
      void readyRead(const QHttpResponseHeader&);
      

      I aim at finishing this task using the current version of Qt.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @tomy

      If memory serves me right, then that would be the QNetworkReply that the QNetworkManager returns on the get request.


      edit: it is

      0_1553149011491_8f93740d-902d-4b3d-8c03-bbdb68edf4f0-image.png


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      tomyT 1 Reply Last reply
      5
      • J.HilkJ J.Hilk

        @tomy

        If memory serves me right, then that would be the QNetworkReply that the QNetworkManager returns on the get request.


        edit: it is

        0_1553149011491_8f93740d-902d-4b3d-8c03-bbdb68edf4f0-image.png

        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3

        @J.Hilk
        So I use the two methods this way:

        void responseHeaderReceived(const QNetworkRequest&);
        void readyRead(const QNetworkRequest&);
        

        Agree, please?

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

          Hi,

          How are you using them ?

          Note that you should use the reply not the request for the readyRead.

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

          tomyT 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            How are you using them ?

            Note that you should use the reply not the request for the readyRead.

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @SGaist

            Yes, agree, readyRead was redundant.
            I need to pursue the project on the video properly to get what is gotten from the video project at the end.

            Please look, there are some signals for QHttp which differ from the ones on QNetworkAccessManager. There he uses the following signals previously existing in theQHttp header file/library. But QNetworkAccessManager lacks them and has different ones.

            0_1553263215223_1.PNG

            0_1553263205945_2.PNG

            Afterwards, he uses those signals in the header file as follows:

            0_1553263305269_3.PNG

            And the question is, while I'm using QNetworkAccessManager instead of QHttp and QNetworkAccessManager hasn't those signals, what should I do?

            And I don't think the following header file is rightly written:

            #ifndef DOWNLOADER_H
            #define DOWNLOADER_H
            
            #include <QObject>
            #include <QNetworkAccessManager>
            #include <QFile>
            #include <QDebug>
            
            class Downloader : public QObject
            {
                Q_OBJECT
            public:
                explicit Downloader(QObject *parent = nullptr);
            
                void Do_download();
            
            signals:
            
            public slots:
            
                void stateChanged(int);
                void responseHeaderReceived(const QNetworkRequest&);
                void requestFinished(int, bool);
            
            private:
                QNetworkAccessManager* http;
            };
            
            #endif // DOWNLOADER_H
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              While both allow network related operations, they are unrelated.

              What exactly are you after with these slots ?

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

              tomyT 1 Reply Last reply
              0
              • SGaistS SGaist

                While both allow network related operations, they are unrelated.

                What exactly are you after with these slots ?

                tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by tomy
                #7

                @SGaist
                Hi,

                While both allow network related operations, they are unrelated.

                What "they/both" are your referring to, please? (The libraries?)

                What exactly are you after with these slots ?

                I don't understand this question. Sorry.

                Dear, I'm trying to start learning Qt network/socket programming, as a beginner of that. So I need to follow and perform the tasks mentioned in the video.

                Do you know of better tutorial for a beginner to "start learning Qt network/socket programming"? (I prefer those videos)

                1 Reply Last reply
                0
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  I'm sorry, but I'm still don't know exactly what you want to do.

                  let me give you a very, very basic (no error management for once) example that you could use to download files from an URL either to a file or to a QByteArray. It's an excerpt from one of my older classes I still have on my HD:

                  #ifndef FILEDOWNLOAD_H
                  #define FILEDOWNLOAD_H
                  
                  #include <QObject>
                  #include <QNetworkAccessManager>
                  #include <QNetworkReply>
                  #include <QFile>
                  
                  class FileDownload : public QObject
                  {
                      Q_OBJECT
                      
                  public:
                      explicit FileDownload(const QString &destination, QUrl url, QObject *parent = nullptr) : QObject(parent)
                      {
                          auto *manager = new QNetworkAccessManager(this);
                          connect(manager, &QNetworkAccessManager::finished, this, [&destination](QNetworkReply* reply)->void {
                              QFile file(destination);
                              if(file.open(QIODevice::WriteOnly)){
                                  QByteArray data = reply->readAll();
                                  file.write(data);
                                  file.close();
                              }
                              reply->deleteLater();
                          });
                          connect(manager, &QNetworkAccessManager::finished, this, &FileDownload::fileDownloaded);
                          connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);
                          
                          auto reply = manager->get(QNetworkRequest(url));
                          connect(reply, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress);
                      }
                      
                      explicit FileDownload(QByteArray &dst, QUrl url, QObject *parent = nullptr) : QObject(parent)
                      {
                          auto *manager = new QNetworkAccessManager(this);
                          connect(manager, &QNetworkAccessManager::finished, this, [&dst](QNetworkReply* reply)->void {
                              dst = reply->readAll();
                              reply->deleteLater();
                          });
                          
                          connect(manager, &QNetworkAccessManager::finished, this, &FileDownload::fileDownloaded);
                          connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);
                          
                          auto reply = manager->get(QNetworkRequest(url));
                          connect(reply, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress);
                      }
                      
                      
                  signals:
                      void downloadProgress(qint64,qint64);
                      void fileDownloaded();
                  };
                         
                  #endif // FILEDOWNLOAD_H
                  

                  that said, Qt comes with a rather good and flushed out Http example, using QNetworkAccessManager

                  https://doc.qt.io/qt-5/qtnetwork-http-example.html

                  did you miss that one ?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  tomyT 1 Reply Last reply
                  2
                  • J.HilkJ J.Hilk

                    I'm sorry, but I'm still don't know exactly what you want to do.

                    let me give you a very, very basic (no error management for once) example that you could use to download files from an URL either to a file or to a QByteArray. It's an excerpt from one of my older classes I still have on my HD:

                    #ifndef FILEDOWNLOAD_H
                    #define FILEDOWNLOAD_H
                    
                    #include <QObject>
                    #include <QNetworkAccessManager>
                    #include <QNetworkReply>
                    #include <QFile>
                    
                    class FileDownload : public QObject
                    {
                        Q_OBJECT
                        
                    public:
                        explicit FileDownload(const QString &destination, QUrl url, QObject *parent = nullptr) : QObject(parent)
                        {
                            auto *manager = new QNetworkAccessManager(this);
                            connect(manager, &QNetworkAccessManager::finished, this, [&destination](QNetworkReply* reply)->void {
                                QFile file(destination);
                                if(file.open(QIODevice::WriteOnly)){
                                    QByteArray data = reply->readAll();
                                    file.write(data);
                                    file.close();
                                }
                                reply->deleteLater();
                            });
                            connect(manager, &QNetworkAccessManager::finished, this, &FileDownload::fileDownloaded);
                            connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);
                            
                            auto reply = manager->get(QNetworkRequest(url));
                            connect(reply, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress);
                        }
                        
                        explicit FileDownload(QByteArray &dst, QUrl url, QObject *parent = nullptr) : QObject(parent)
                        {
                            auto *manager = new QNetworkAccessManager(this);
                            connect(manager, &QNetworkAccessManager::finished, this, [&dst](QNetworkReply* reply)->void {
                                dst = reply->readAll();
                                reply->deleteLater();
                            });
                            
                            connect(manager, &QNetworkAccessManager::finished, this, &FileDownload::fileDownloaded);
                            connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);
                            
                            auto reply = manager->get(QNetworkRequest(url));
                            connect(reply, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress);
                        }
                        
                        
                    signals:
                        void downloadProgress(qint64,qint64);
                        void fileDownloaded();
                    };
                           
                    #endif // FILEDOWNLOAD_H
                    

                    that said, Qt comes with a rather good and flushed out Http example, using QNetworkAccessManager

                    https://doc.qt.io/qt-5/qtnetwork-http-example.html

                    did you miss that one ?

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by tomy
                    #9

                    @J.Hilk
                    Dear Hilk, I don't know why what I said is ambiguous yet, but let me re-say that the simplest way I have in mind for now:

                    Please look at that video. It's outdated, isn't it? So try to update it using the current version of Qt. That is, we want to follow that video, step-by-step, using the slots used there, but with only one difference. That difference is that we use the slots/signals/methods' equivalent names in Qt 5.12. (Because we can't run that project using this 5.12 version of Qt)
                    So if I know all the new names, I can run that exact project (on the video) using my current Qt. For instance, the equivalent of "QHttp" is "QNetworkAccessManager". How about the name of other ones in 5.12, especially the slots' names, used in the video?

                    If it's still unclear, please tell me. Then I try to clarify it. Afterwards, we can help each other.

                    J.HilkJ 1 Reply Last reply
                    0
                    • tomyT tomy

                      @J.Hilk
                      Dear Hilk, I don't know why what I said is ambiguous yet, but let me re-say that the simplest way I have in mind for now:

                      Please look at that video. It's outdated, isn't it? So try to update it using the current version of Qt. That is, we want to follow that video, step-by-step, using the slots used there, but with only one difference. That difference is that we use the slots/signals/methods' equivalent names in Qt 5.12. (Because we can't run that project using this 5.12 version of Qt)
                      So if I know all the new names, I can run that exact project (on the video) using my current Qt. For instance, the equivalent of "QHttp" is "QNetworkAccessManager". How about the name of other ones in 5.12, especially the slots' names, used in the video?

                      If it's still unclear, please tell me. Then I try to clarify it. Afterwards, we can help each other.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @tomy said in My first Qt socket programming program:

                      That is, we want to follow that video, step-by-step, using the slots used there, but with only one difference. That difference is that we use the slots/signals/methods' equivalent names in Qt 5.12.

                      Ähm no, the major difference is that QHttp is no longer part of the Qt5 Library. QNetworkAccessManager already existed in Qt4.

                      You will not be able to (well simply) recreate that tutorial in Qt5. The concepts of states and the readyread signal, AFAIK does not exist in QNetworkAccessManager class. QNAM is more abstract and higher level than that.

                      You could potentially do it via a QSslSocket but you would have to implement everything yourself.

                      But there's a reason why QNAM replaced stuff like QHttp and QFtp is much, much simpler to use.

                      In the end, what Brians program ends up doing, is exactly what my and the example from the docs do:

                      Provide a way to download files from the internet.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      tomyT 1 Reply Last reply
                      2
                      • J.HilkJ J.Hilk

                        @tomy said in My first Qt socket programming program:

                        That is, we want to follow that video, step-by-step, using the slots used there, but with only one difference. That difference is that we use the slots/signals/methods' equivalent names in Qt 5.12.

                        Ähm no, the major difference is that QHttp is no longer part of the Qt5 Library. QNetworkAccessManager already existed in Qt4.

                        You will not be able to (well simply) recreate that tutorial in Qt5. The concepts of states and the readyread signal, AFAIK does not exist in QNetworkAccessManager class. QNAM is more abstract and higher level than that.

                        You could potentially do it via a QSslSocket but you would have to implement everything yourself.

                        But there's a reason why QNAM replaced stuff like QHttp and QFtp is much, much simpler to use.

                        In the end, what Brians program ends up doing, is exactly what my and the example from the docs do:

                        Provide a way to download files from the internet.

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by tomy
                        #11

                        @J.Hilk

                        Yeah, that video might be doing the same or even better thing as that video, but it's rather hard for me to understand!
                        I took a look at that example. Its .cpp file solely, contains about 300 lines of code. It's too advanced for a beginner like me.

                        Up to now, I've never written even one line of code for socket/network programming. That's why I'm more interested in the Bryan's project because I think it's simpler. But now I've got bogged down!

                        What do you suggest please, if you want to guide me?
                        My goal? Yes, my goal is learning socket/network programming using Qt as an absolute beginner.

                        JonBJ J.HilkJ 2 Replies Last reply
                        0
                        • tomyT tomy

                          @J.Hilk

                          Yeah, that video might be doing the same or even better thing as that video, but it's rather hard for me to understand!
                          I took a look at that example. Its .cpp file solely, contains about 300 lines of code. It's too advanced for a beginner like me.

                          Up to now, I've never written even one line of code for socket/network programming. That's why I'm more interested in the Bryan's project because I think it's simpler. But now I've got bogged down!

                          What do you suggest please, if you want to guide me?
                          My goal? Yes, my goal is learning socket/network programming using Qt as an absolute beginner.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @tomy
                          Although I am not the person to advise you on what videos/tutorials to use, perhaps it is time to accept that one, which is years old and completely does not work in Qt5, is simply not the right one to persist with now. If you read the comments to the youtube video you already see lots of "this doesn't work with Qt5, how do you do it using QNetworkReply ?", indicating that others are aware this is not a suitable starting point nowadays, and may not be a good idea for beginners....

                          1 Reply Last reply
                          3
                          • tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by
                            #13

                            OK, but what other tutorial can I use instead? If it's even not video, it's fine yet. Or even any other tutorial which mighn't be very beginner-friendly but I can get help from here to make progress in that. That would be fine and useful too. All in all, I need to somewhere start learning socket/network programming using Qt. What do you suggest please?

                            Bryan's videos are updated and published in bout three months from now and I can't wait until that time unfortunately.

                            aha_1980A 1 Reply Last reply
                            0
                            • tomyT tomy

                              @J.Hilk

                              Yeah, that video might be doing the same or even better thing as that video, but it's rather hard for me to understand!
                              I took a look at that example. Its .cpp file solely, contains about 300 lines of code. It's too advanced for a beginner like me.

                              Up to now, I've never written even one line of code for socket/network programming. That's why I'm more interested in the Bryan's project because I think it's simpler. But now I've got bogged down!

                              What do you suggest please, if you want to guide me?
                              My goal? Yes, my goal is learning socket/network programming using Qt as an absolute beginner.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @tomy said in My first Qt socket programming program:

                              What do you suggest please, if you want to guide me?
                              My goal? Yes, my goal is learning socket/network programming using Qt as an absolute beginner.

                              I'm afraid, I can't recommend much here. Because it's a very broad subject. Qt has all the tools necessary for any Network or Socket programming, but you'll need to know the fundamentals behind it first.

                              If you don't have those, then it's going to be tough.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1
                              • C Offline
                                C Offline
                                careerjobs360
                                Banned
                                wrote on last edited by
                                #15
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • tomyT tomy

                                  OK, but what other tutorial can I use instead? If it's even not video, it's fine yet. Or even any other tutorial which mighn't be very beginner-friendly but I can get help from here to make progress in that. That would be fine and useful too. All in all, I need to somewhere start learning socket/network programming using Qt. What do you suggest please?

                                  Bryan's videos are updated and published in bout three months from now and I can't wait until that time unfortunately.

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @tomy What about https://www.youtube.com/watch?v=_gSC60y8woA ?

                                  Qt has to stay free or it will die.

                                  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