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.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.
  • S SGaist
    22 Mar 2019, 20:54

    While both allow network related operations, they are unrelated.

    What exactly are you after with these slots ?

    T Offline
    T Offline
    tomy
    wrote on 22 Mar 2019, 21:03 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 Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 22 Mar 2019, 21:56 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.

      T 1 Reply Last reply 22 Mar 2019, 22:15
      2
      • J J.Hilk
        22 Mar 2019, 21:56

        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 ?

        T Offline
        T Offline
        tomy
        wrote on 22 Mar 2019, 22:15 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 1 Reply Last reply 22 Mar 2019, 22:34
        0
        • T tomy
          22 Mar 2019, 22:15

          @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 Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 22 Mar 2019, 22:34 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.

          T 1 Reply Last reply 22 Mar 2019, 22:44
          2
          • J J.Hilk
            22 Mar 2019, 22:34

            @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.

            T Offline
            T Offline
            tomy
            wrote on 22 Mar 2019, 22:44 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.

            J J 2 Replies Last reply 23 Mar 2019, 10:12
            0
            • T tomy
              22 Mar 2019, 22:44

              @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 Offline
              J Offline
              JonB
              wrote on 23 Mar 2019, 10:12 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
              • T Offline
                T Offline
                tomy
                wrote on 23 Mar 2019, 20:51 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.

                A 1 Reply Last reply 25 Mar 2019, 07:01
                0
                • T tomy
                  22 Mar 2019, 22:44

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 25 Mar 2019, 05:46 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 25 Mar 2019, 06:38 last edited by
                    #15
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • T tomy
                      23 Mar 2019, 20:51

                      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.

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 25 Mar 2019, 07:01 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

                      16/16

                      25 Mar 2019, 07:01

                      • Login

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