help!: how to display image through http url
-
wrote on 21 Jan 2021, 04:32 last edited by
i used QString :
QString url = R"(https://..)";
QPixmap img(url);
QLabel *label2 = new QLabel(this);
label2->setPixmap(img);tks all!
-
i used QString :
QString url = R"(https://..)";
QPixmap img(url);
QLabel *label2 = new QLabel(this);
label2->setPixmap(img);tks all!
Hi @nguyenhuy, and welcome!
In C++, you must first download the file using
QNetworkAccessManager::get()
. Then, useQImage::loadFromData()
to convert the downloaded bytes into an image. -
Hi @nguyenhuy, and welcome!
In C++, you must first download the file using
QNetworkAccessManager::get()
. Then, useQImage::loadFromData()
to convert the downloaded bytes into an image. -
i used QString :
QString url = R"(https://..)";
QPixmap img(url);
QLabel *label2 = new QLabel(this);
label2->setPixmap(img);tks all!
@nguyenhuy You could always change to QML, the Image component will do that for you automatically
One of the 2 really good features of QML that I like, Image and Text, so much better than their c++ counter parts
-
wrote on 22 Jan 2021, 07:54 last edited by nguyenhuy 2 May 2021, 05:04
i have created a standard mainboard in qt-creator. and suplied it with the example filedownloader from:
"https://wiki.qt.io/Download_Data_from_URL"
but error: ‘m_pImgCtrl’ was not declared in this scope
and use of undeclared identifier 'm_pImgCtrl'
i don't understand :( -
i have created a standard mainboard in qt-creator. and suplied it with the example filedownloader from:
"https://wiki.qt.io/Download_Data_from_URL"
but error: ‘m_pImgCtrl’ was not declared in this scope
and use of undeclared identifier 'm_pImgCtrl'
i don't understand :(@nguyenhuy said in help!: how to display image through http url:
but error: ‘m_pImgCtrl’ was not declared in this scope
and use of undeclared identifier 'm_pImgCtrl'Please show your code.
-
i have created a standard mainboard in qt-creator. and suplied it with the example filedownloader from:
"https://wiki.qt.io/Download_Data_from_URL"
but error: ‘m_pImgCtrl’ was not declared in this scope
and use of undeclared identifier 'm_pImgCtrl'
i don't understand :(Hi,
@nguyenhuy said in help!: how to display image through http url:
i have created a standard mainboard in qt-creator. and suplied it with the example filedownloader from:
"http://developer.qt.nokia.com/wiki/Download_Data_from_URL"You might want to update your links, this one has been dead for quite a while now.
-
wrote on 22 Jan 2021, 08:35 last edited by J.Hilk
here @jsulm tks :)
**filedownloader.h:** #ifndef FILEDOWNLOADER_H #define FILEDOWNLOADER_H #include <QObject> #include <QByteArray> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QUrl> class filedownloader : public QObject { public: explicit filedownloader(QUrl imageUrl, QObject); virtual ~filedownloader(); QByteArray downloadedData() const; signals: void downloaded(); private slots: void fileDownloaded(QNetworkReply pReply); private: QNetworkAccessManager m_WebCtrl; QByteArray m_DownloadedData; }; #endif // FILEDOWNLOADER_H **mainwindow.h:** #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtWidgets/QMainWindow> #include <QByteArray> namespace Ui { class MainWindow; } class mainwindow :public QMainWindow { Q_OBJECT public: explicit mainwindow(QWidget *parent = 0); ~mainwindow(); private: Ui::MainWindow *ui; private slots: void loadImage(); void on_pushButton_clicked(); }; #endif // MAINWINDOW_H **filedownloader.cpp:** #include "filedownloader.h" filedownloader::filedownloader(QUrl imageUrl,QObject parent) { connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply)), SLOT(fileDownloaded(QNetworkReply*))); QNetworkRequest request(imageUrl); m_WebCtrl.get(request); } filedownloader::~filedownloader() { } void filedownloader::fileDownloaded(QNetworkReply pReply) { m_DownloadedData = pReply.readAll(); emit downloaded(); } QByteArray filedownloader::downloadedData() const { return m_DownloadedData; } **mainwindow.cpp:** #include "mainwindow.h" #include "ui_mainwindow.h" #include "filedownloader.h" #include "QUrl" mainwindow::mainwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QUrl imageUrl("..."); m_pImgCtrl = new fileDownloaded(imageUrl, this); connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage())); } mainwindow::~mainwindow() { delete ui; } void mainwindow::loadImage() { QPixmap buttonImage; buttonImage.loadFromData(m_pImgCtrl->downloadedData()); } **main:** #include <QCoreApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); mainwindow w; w.show(); return a.exec(); }
Added proper code tags [ @J-Hilk ]
-
Please use the code tags to make your code readable - currently noone can read anything in your code.
-
here @jsulm tks :)
**filedownloader.h:** #ifndef FILEDOWNLOADER_H #define FILEDOWNLOADER_H #include <QObject> #include <QByteArray> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QUrl> class filedownloader : public QObject { public: explicit filedownloader(QUrl imageUrl, QObject); virtual ~filedownloader(); QByteArray downloadedData() const; signals: void downloaded(); private slots: void fileDownloaded(QNetworkReply pReply); private: QNetworkAccessManager m_WebCtrl; QByteArray m_DownloadedData; }; #endif // FILEDOWNLOADER_H **mainwindow.h:** #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtWidgets/QMainWindow> #include <QByteArray> namespace Ui { class MainWindow; } class mainwindow :public QMainWindow { Q_OBJECT public: explicit mainwindow(QWidget *parent = 0); ~mainwindow(); private: Ui::MainWindow *ui; private slots: void loadImage(); void on_pushButton_clicked(); }; #endif // MAINWINDOW_H **filedownloader.cpp:** #include "filedownloader.h" filedownloader::filedownloader(QUrl imageUrl,QObject parent) { connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply)), SLOT(fileDownloaded(QNetworkReply*))); QNetworkRequest request(imageUrl); m_WebCtrl.get(request); } filedownloader::~filedownloader() { } void filedownloader::fileDownloaded(QNetworkReply pReply) { m_DownloadedData = pReply.readAll(); emit downloaded(); } QByteArray filedownloader::downloadedData() const { return m_DownloadedData; } **mainwindow.cpp:** #include "mainwindow.h" #include "ui_mainwindow.h" #include "filedownloader.h" #include "QUrl" mainwindow::mainwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QUrl imageUrl("..."); m_pImgCtrl = new fileDownloaded(imageUrl, this); connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage())); } mainwindow::~mainwindow() { delete ui; } void mainwindow::loadImage() { QPixmap buttonImage; buttonImage.loadFromData(m_pImgCtrl->downloadedData()); } **main:** #include <QCoreApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); mainwindow w; w.show(); return a.exec(); }
Added proper code tags [ @J-Hilk ]
wrote on 22 Jan 2021, 08:50 last edited by@nguyenhuy
As @Christian-Ehrlicher has said, please make the effort to use the forum's Code tags button.Meanwhile, as per the error message, this code uses
m_pImgCtrl
which looks like a member variable but is not declared in the class in the.h
file. -
Please use the code tags to make your code readable - currently noone can read anything in your code.
wrote on 22 Jan 2021, 08:51 last edited by@Christian-Ehrlicher oh~ sr i'm newbiee
-
Hi,
@nguyenhuy said in help!: how to display image through http url:
i have created a standard mainboard in qt-creator. and suplied it with the example filedownloader from:
"http://developer.qt.nokia.com/wiki/Download_Data_from_URL"You might want to update your links, this one has been dead for quite a while now.
-
@nguyenhuy said in help!: how to display image through http url:
@SGaist yes please
That's up to you to update them, you found them in the first place.
-
@nguyenhuy
As @Christian-Ehrlicher has said, please make the effort to use the forum's Code tags button.Meanwhile, as per the error message, this code uses
m_pImgCtrl
which looks like a member variable but is not declared in the class in the.h
file.wrote on 27 Jan 2021, 03:45 last edited byi tried i out with
@QbyteArray m_pImgCtrl; -
wrote on 27 Jan 2021, 07:08 last edited by JonB
@nguyenhuy
And?I think you should be looking at https://wiki.qt.io/Download_Data_from_URL rather than your deceased nokia link. There you will see
m_pImgCtrl = new FileDownloader(imageUrl, this);
so you are supposed to figure for yourself that the member variable declaration will need to be
FileDownloader *m_pImgCtrl;
-
@nguyenhuy
And?I think you should be looking at https://wiki.qt.io/Download_Data_from_URL rather than your deceased nokia link. There you will see
m_pImgCtrl = new FileDownloader(imageUrl, this);
so you are supposed to figure for yourself that the member variable declaration will need to be
FileDownloader *m_pImgCtrl;
-
@nguyenhuy Did you include filedownloader.h?
-
@nguyenhuy Did you include filedownloader.h?
wrote on 1 Feb 2021, 06:41 last edited by@jsulm said in help!: how to display image through http url:
Did you include filedownloader.h
yes, sir
-
@jsulm said in help!: how to display image through http url:
Did you include filedownloader.h
yes, sir
@nguyenhuy Please show your current code
-
@nguyenhuy Please show your current code
wrote on 1 Feb 2021, 06:58 last edited by@jsulm ```
code_text
mainwindow.h#define MAINWINDOW_H #include <QtWidgets/QMainWindow> namespace Ui { class MainWindow; } class mainwindow :public QMainWindow { Q_OBJECT public: explicit mainwindow(QWidget *parent = 0); ~mainwindow(); signals: void downloaded(); private slots: void loadImage(); void on_pushButton_clicked(); private: Ui::MainWindow *ui; FileDownloader *m_pImgCtrl; }; #endif // MAINWINDOW_H mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "filedownloader.h" #include "QUrl" #include <QtWidgets/QMainWindow> mainwindow::mainwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QUrl imageUrl("..."); m_pImgCtrl = new FileDownloader(imageUrl, this); connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage())); } mainwindow::~mainwindow() { delete ui; } void mainwindow::loadImage() { QPixmap buttonImage; buttonImage.loadFromData(m_pImgCtrl->downloadedData()); }
1/21