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

Draw QImage in QOpenGLWidget

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 8 Posters 8.0k Views 4 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.
  • Z Offline
    Z Offline
    Zarcoin
    wrote on 13 Jan 2021, 11:06 last edited by
    #21

    Woaw big thanks to you @mrjj for the explanation! So i did what you said, but unfortunately it's still not working. oof
    As you can see here i added an rqc and my file.
    7216fa03-ec3c-4d78-83be-9394e2afcdf2-image.png

    and i then wrote this code:

    void MapOpenGL::initializeGL(){
        bg = new QImage();
        qDebug() << QFile::exists("mulhouse.PNG");
    }
    
    void MapOpenGL::paintGL(){
        QPainter p(this);
    
        p.setPen(Qt::white);
    
        bool test = bg->load(QString(":/mulhouse.PNG"));
        if (test == false){
            p.drawLine(QLineF(10, 10, 500, 500));
            p.drawLine(QLineF(500, 10, 10, 500));
        }else{
            QRectF target( 10, 10, 409, 210);
            //QRectF source( 0.0, 0.0, 399, 200);
            p.drawImage( target, *bg);
        }
    }
    

    It still draws lines instead of the image and debug says false !
    Should i add a prefix in my rqc file ? how does it work exatcely in qtcreator ?

    Zarcoin.
    The Duck thanks you and wish you a good day !

    A M 2 Replies Last reply 13 Jan 2021, 11:08
    0
    • Z Zarcoin
      13 Jan 2021, 11:06

      Woaw big thanks to you @mrjj for the explanation! So i did what you said, but unfortunately it's still not working. oof
      As you can see here i added an rqc and my file.
      7216fa03-ec3c-4d78-83be-9394e2afcdf2-image.png

      and i then wrote this code:

      void MapOpenGL::initializeGL(){
          bg = new QImage();
          qDebug() << QFile::exists("mulhouse.PNG");
      }
      
      void MapOpenGL::paintGL(){
          QPainter p(this);
      
          p.setPen(Qt::white);
      
          bool test = bg->load(QString(":/mulhouse.PNG"));
          if (test == false){
              p.drawLine(QLineF(10, 10, 500, 500));
              p.drawLine(QLineF(500, 10, 10, 500));
          }else{
              QRectF target( 10, 10, 409, 210);
              //QRectF source( 0.0, 0.0, 399, 200);
              p.drawImage( target, *bg);
          }
      }
      

      It still draws lines instead of the image and debug says false !
      Should i add a prefix in my rqc file ? how does it work exatcely in qtcreator ?

      A Offline
      A Offline
      artwaw
      wrote on 13 Jan 2021, 11:08 last edited by
      #22

      @Zarcoin your resource file seems to be empty/not include any files. However I can see you've included the png file in your project...
      Right-click on the resource file, select "open editor" or similar. Then add the file to the resource file. All we said about prefixes will become self-explanatory once you open the editor I hope.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      2
      • Z Zarcoin
        13 Jan 2021, 11:06

        Woaw big thanks to you @mrjj for the explanation! So i did what you said, but unfortunately it's still not working. oof
        As you can see here i added an rqc and my file.
        7216fa03-ec3c-4d78-83be-9394e2afcdf2-image.png

        and i then wrote this code:

        void MapOpenGL::initializeGL(){
            bg = new QImage();
            qDebug() << QFile::exists("mulhouse.PNG");
        }
        
        void MapOpenGL::paintGL(){
            QPainter p(this);
        
            p.setPen(Qt::white);
        
            bool test = bg->load(QString(":/mulhouse.PNG"));
            if (test == false){
                p.drawLine(QLineF(10, 10, 500, 500));
                p.drawLine(QLineF(500, 10, 10, 500));
            }else{
                QRectF target( 10, 10, 409, 210);
                //QRectF source( 0.0, 0.0, 399, 200);
                p.drawImage( target, *bg);
            }
        }
        

        It still draws lines instead of the image and debug says false !
        Should i add a prefix in my rqc file ? how does it work exatcely in qtcreator ?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 13 Jan 2021, 11:12 last edited by mrjj
        #23

        @Zarcoin

        Hi
        Try again, you right-click the wrong place. then it adds as other files.
        Right click the actual qrc file in the tree.
        You can have more than one qres file so one right-clicks the one , that file should be added to.

        So once you get the file to be in res "folder", you can right-click the file and take the path so its always 100% correct.

        1 Reply Last reply
        2
        • Z Offline
          Z Offline
          Zarcoin
          wrote on 13 Jan 2021, 11:25 last edited by
          #24

          It works ! I didn't understand that i had to create a prefix and then add the file ! after that i can indeed right click on the file and copy the path

          Thank you so much to all of you for the help, you're so nice !

          Zarcoin.
          The Duck thanks you and wish you a good day !

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zarcoin
            wrote on 13 Jan 2021, 11:36 last edited by Zarcoin
            #25

            For people asking the same question, here is my result (using QtCreator):

            • Create a new class that extends OpenGlWidget
            class MapOpenGL : public QOpenGLWidget
            {
            public:
                MapOpenGL( QWidget* parent = NULL);
            
                virtual void initializeGL();
                virtual void paintGL();
                virtual void resizeGL( int width, int heigh);
            };
            
            • In the ui, right click your OpenGLWidget, click on "promote to" and then use your new class
            • then you need to add your resource in the project. File -> New File -> Qt -> Qt Resource File
            • right click your qrc file and open editor. Create a prefix, then create a ressource file ( the file you wanna draw)
            • In your qrc sub folder, find your image, right click and copy the path
            • I then use QPainter to draw my image:
            MapOpenGL::MapOpenGL( QWidget* parent) : QOpenGLWidget (parent)
            {
            
            }
            
            void MapOpenGL::initializeGL(){
            }
            
            void MapOpenGL::paintGL(){
                QPainter p(this);
                QRectF target( 10, 10, 500, 500);
                p.drawImage( target, new QImage(QString("path_of_the_image_you_copied"));
            
            }
            

            For further information on how to draw, go check QPainter methods.

            Zarcoin.
            The Duck thanks you and wish you a good day !

            1 Reply Last reply
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 Jan 2021, 18:24 last edited by
              #26

              @Zarcoin said in Draw QImage in QOpenGLWidget:

              bg = new QImage();

              No need to allocate QImage on the heap. You are just adding the overhead of dereferencing it in each call of paintGL.

              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
              2
              • Z Offline
                Z Offline
                Zarcoin
                wrote on 14 Jan 2021, 09:29 last edited by
                #27

                Yes don't worry that's what i did, i just wanted to show how it works ! But thank you tho !

                Zarcoin.
                The Duck thanks you and wish you a good day !

                Q 1 Reply Last reply 8 Jan 2022, 01:05
                0
                • Z Zarcoin
                  14 Jan 2021, 09:29

                  Yes don't worry that's what i did, i just wanted to show how it works ! But thank you tho !

                  Q Offline
                  Q Offline
                  QtTester
                  wrote on 8 Jan 2022, 01:05 last edited by
                  #28

                  @Zarcoin
                  I never thought you can use QPainter in ::paintGL(), that's a new idea. have you compared the CPU usage with ::paintEvent() ?

                  kshegunovK 1 Reply Last reply 9 Jan 2022, 12:13
                  0
                  • Q QtTester
                    8 Jan 2022, 01:05

                    @Zarcoin
                    I never thought you can use QPainter in ::paintGL(), that's a new idea. have you compared the CPU usage with ::paintEvent() ?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on 9 Jan 2022, 12:13 last edited by
                    #29

                    @QtTester said in Draw QImage in QOpenGLWidget:

                    I never thought you can use QPainter in ::paintGL(), that's a new idea. have you compared the CPU usage with ::paintEvent() ?

                    There's no paintEvent with OpenGL widget, or specifically you shouldn't mess with it unless you want to break it. paintEvent is going to call the paintGL whenever it's ready for you to issue the draw commands or w/e. As for the CPU usage it probably is comparable.

                    Note: please don't do the "stand up zombie" without a good reason. I really doubt the OP will be back to answer you.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0

                    • Login

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