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
Forum Updated to NodeBB v4.3 + New Features

help!: how to display image through http url

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 7 Posters 7.3k 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.
  • JKSHJ JKSH

    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.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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

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

      tks all!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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 last edited by nguyenhuy
        #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 :(

        jsulmJ SGaistS 2 Replies Last reply
        0
        • N nguyenhuy

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

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

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            0
            • N Offline
              N Offline
              nguyenhuy
              wrote on 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 ]

              JonBJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                2
                • N nguyenhuy

                  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 ]

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    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 last edited by
                    #11

                    @Christian-Ehrlicher oh~ sr i'm newbiee

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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 last edited by
                      #12

                      @SGaist yes please

                      SGaistS 1 Reply Last reply
                      0
                      • N nguyenhuy

                        @SGaist yes please

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        • JonBJ JonB

                          @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 last edited by
                          #14

                          i tried i out with
                          @QbyteArray m_pImgCtrl;

                          JonBJ 1 Reply Last reply
                          0
                          • N nguyenhuy

                            i tried i out with
                            @QbyteArray m_pImgCtrl;

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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
                            3
                            • JonBJ 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;
                              
                              N Offline
                              N Offline
                              nguyenhuy
                              wrote on last edited by
                              #16

                              @JonB unknown type name FileDownloader

                              jsulmJ 1 Reply Last reply
                              0
                              • N nguyenhuy

                                @JonB unknown type name FileDownloader

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 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
                                0
                                • jsulmJ jsulm

                                  @nguyenhuy Did you include filedownloader.h?

                                  N Offline
                                  N Offline
                                  nguyenhuy
                                  wrote on last edited by
                                  #18

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

                                  Did you include filedownloader.h

                                  yes, sir

                                  jsulmJ 1 Reply Last reply
                                  -1
                                  • N nguyenhuy

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

                                    Did you include filedownloader.h

                                    yes, sir

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 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
                                    0
                                    • jsulmJ jsulm

                                      @nguyenhuy Please show your current code

                                      N Offline
                                      N Offline
                                      nguyenhuy
                                      wrote on 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());
                                      
                                      }
                                      jsulmJ 1 Reply Last reply
                                      0
                                      • N nguyenhuy

                                        @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());
                                        
                                        }
                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        @nguyenhuy Why did you say that you included filedownloader.h if you did not?!
                                        You have to include it in mainwindow.h as you use it there. Or, as an alternative, make a forward declaration of FileDownloader in mainwindow.h and include filedownloader.h in mainwindow.cpp.
                                        This is basic C++ knowledge...

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

                                        1 Reply Last reply
                                        3

                                        • Login

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