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. help!: how to display image through http url

help!: how to display image through http url

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 7 Posters 7.0k Views
  • 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.
  • N Offline
    N Offline
    nguyenhuy
    wrote on 21 Jan 2021, 04:32 last edited by
    #1

    i used QString :
    QString url = R"(https://..)";
    QPixmap img(url);
    QLabel *label2 = new QLabel(this);
    label2->setPixmap(img);

    tks all!

    J J 2 Replies Last reply 21 Jan 2021, 05:13
    0
    • N nguyenhuy
      21 Jan 2021, 04:32

      i used QString :
      QString url = R"(https://..)";
      QPixmap img(url);
      QLabel *label2 = new QLabel(this);
      label2->setPixmap(img);

      tks all!

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Jan 2021, 05:13 last edited by
      #2

      Hi @nguyenhuy, and welcome!

      In C++, you must first download the file using QNetworkAccessManager::get(). Then, use QImage::loadFromData() to convert the downloaded bytes into an image.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      J 1 Reply Last reply 21 Jan 2021, 07:01
      2
      • J JKSH
        21 Jan 2021, 05:13

        Hi @nguyenhuy, and welcome!

        In C++, you must first download the file using QNetworkAccessManager::get(). Then, use QImage::loadFromData() to convert the downloaded bytes into an image.

        J Offline
        J Offline
        JonB
        wrote on 21 Jan 2021, 07:01 last edited by JonB
        #3

        @JKSH
        I haven't looked, but can you also do this in a Qt desktop app via stylesheet and something like background-image: url(...), which would be simpler?

        1 Reply Last reply
        0
        • N nguyenhuy
          21 Jan 2021, 04:32

          i used QString :
          QString url = R"(https://..)";
          QPixmap img(url);
          QLabel *label2 = new QLabel(this);
          label2->setPixmap(img);

          tks all!

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 21 Jan 2021, 07:05 last edited by
          #4

          @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


          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
          2
          • N Offline
            N Offline
            nguyenhuy
            wrote on 22 Jan 2021, 07:54 last edited by nguyenhuy 2 May 2021, 05:04
            #5

            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 :(

            J S 2 Replies Last reply 22 Jan 2021, 08:00
            0
            • N nguyenhuy
              22 Jan 2021, 07:54

              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 :(

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 22 Jan 2021, 08:00 last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N nguyenhuy
                22 Jan 2021, 07:54

                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 :(

                S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 22 Jan 2021, 08:05 last edited by
                #7

                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.

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

                N 1 Reply Last reply 22 Jan 2021, 10:25
                0
                • N Offline
                  N Offline
                  nguyenhuy
                  wrote on 22 Jan 2021, 08:35 last edited by J.Hilk
                  #8

                  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 ]

                  J 1 Reply Last reply 22 Jan 2021, 08:50
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 22 Jan 2021, 08:43 last edited by
                    #9

                    Please use the code tags to make your code readable - currently noone can read anything in your code.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    N 1 Reply Last reply 22 Jan 2021, 08:51
                    2
                    • N nguyenhuy
                      22 Jan 2021, 08:35

                      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 ]

                      J Offline
                      J Offline
                      JonB
                      wrote on 22 Jan 2021, 08:50 last edited by
                      #10

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

                      N 1 Reply Last reply 27 Jan 2021, 03:45
                      3
                      • Christian EhrlicherC Christian Ehrlicher
                        22 Jan 2021, 08:43

                        Please use the code tags to make your code readable - currently noone can read anything in your code.

                        N Offline
                        N Offline
                        nguyenhuy
                        wrote on 22 Jan 2021, 08:51 last edited by
                        #11

                        @Christian-Ehrlicher oh~ sr i'm newbiee

                        1 Reply Last reply
                        0
                        • S SGaist
                          22 Jan 2021, 08:05

                          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.

                          N Offline
                          N Offline
                          nguyenhuy
                          wrote on 22 Jan 2021, 10:25 last edited by
                          #12

                          @SGaist yes please

                          S 1 Reply Last reply 22 Jan 2021, 19:34
                          0
                          • N nguyenhuy
                            22 Jan 2021, 10:25

                            @SGaist yes please

                            S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 22 Jan 2021, 19:34 last edited by
                            #13

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

                            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
                            • J JonB
                              22 Jan 2021, 08:50

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

                              N Offline
                              N Offline
                              nguyenhuy
                              wrote on 27 Jan 2021, 03:45 last edited by
                              #14

                              i tried i out with
                              @QbyteArray m_pImgCtrl;

                              J 1 Reply Last reply 27 Jan 2021, 07:08
                              0
                              • N nguyenhuy
                                27 Jan 2021, 03:45

                                i tried i out with
                                @QbyteArray m_pImgCtrl;

                                J Offline
                                J Offline
                                JonB
                                wrote on 27 Jan 2021, 07:08 last edited by JonB
                                #15

                                @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;
                                
                                N 1 Reply Last reply 1 Feb 2021, 03:48
                                3
                                • J JonB
                                  27 Jan 2021, 07:08

                                  @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;
                                  
                                  N Offline
                                  N Offline
                                  nguyenhuy
                                  wrote on 1 Feb 2021, 03:48 last edited by
                                  #16

                                  @JonB unknown type name FileDownloader

                                  J 1 Reply Last reply 1 Feb 2021, 06:25
                                  0
                                  • N nguyenhuy
                                    1 Feb 2021, 03:48

                                    @JonB unknown type name FileDownloader

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 1 Feb 2021, 06:25 last edited by
                                    #17

                                    @nguyenhuy Did you include filedownloader.h?

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    N 1 Reply Last reply 1 Feb 2021, 06:41
                                    0
                                    • J jsulm
                                      1 Feb 2021, 06:25

                                      @nguyenhuy Did you include filedownloader.h?

                                      N Offline
                                      N Offline
                                      nguyenhuy
                                      wrote on 1 Feb 2021, 06:41 last edited by
                                      #18

                                      @jsulm said in help!: how to display image through http url:

                                      Did you include filedownloader.h

                                      yes, sir

                                      J 1 Reply Last reply 1 Feb 2021, 06:42
                                      -1
                                      • N nguyenhuy
                                        1 Feb 2021, 06:41

                                        @jsulm said in help!: how to display image through http url:

                                        Did you include filedownloader.h

                                        yes, sir

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 1 Feb 2021, 06:42 last edited by
                                        #19

                                        @nguyenhuy Please show your current code

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        N 1 Reply Last reply 1 Feb 2021, 06:58
                                        0
                                        • J jsulm
                                          1 Feb 2021, 06:42

                                          @nguyenhuy Please show your current code

                                          N Offline
                                          N Offline
                                          nguyenhuy
                                          wrote on 1 Feb 2021, 06:58 last edited by
                                          #20

                                          @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());
                                          
                                          }
                                          J 1 Reply Last reply 1 Feb 2021, 07:00
                                          0

                                          1/21

                                          21 Jan 2021, 04:32

                                          • Login

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