My first Qt socket programming program
-
Hi all,
I finally started learning Qt socket programming by this video. It's beneficial but outdated.
Here is mydownloader.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 ofQHttp
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.
-
Hi,
How are you using them ?
Note that you should use the reply not the request for the readyRead.
-
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.
Afterwards, he uses those signals in the header file as follows:
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
-
While both allow network related operations, they are unrelated.
What exactly are you after with these slots ?
-
@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)
-
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 ?
-
@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.
-
@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.
-
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. -
@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 usingQNetworkReply
?", indicating that others are aware this is not a suitable starting point nowadays, and may not be a good idea for beginners.... -
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.
-
@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.
-
This post is deleted!
-
@tomy What about https://www.youtube.com/watch?v=_gSC60y8woA ?