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