Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can I draw a png image using QPixmap and QPainter?
QtWS25 Last Chance

How can I draw a png image using QPixmap and QPainter?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.3k 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.
  • S Offline
    S Offline
    sofiane
    wrote on last edited by sofiane
    #1

    I'm trying to draw an image into my widget window, what is the right way of doing, the following code is not displaying anything on the window :

    #include <QCoreApplication>
    #include <QApplication>
    #include <QWidget>
    #include <QPainter>
    #include <QColor>
    #include <QRectF>
    #include <QPicture>
    
    class Win: public QWidget{
    public:
        void paintEvent(QPaintEvent *){
            QPainter painter(this);
    
            QRectF target(10.0, 20.0, 800.0, 600.0);
            QRectF source(0.0, 0.0, 400.0, 400.0);
            QPixmap pixmap("qrc:/browser_web_internet_website.png");
    
            painter.drawPixmap(target, pixmap, source);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Win *win = new Win();
    
        win->show();
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #7

      QLabel

      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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Check that your pixmap is valid.

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

        S 1 Reply Last reply
        0
        • S Offline
          S Offline
          sofiane
          wrote on last edited by sofiane
          #3

          @SGaist What do you mean by valid? the image exists, what I mean by not working is that the window opens but not displaying anything.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Check that your pixmap is valid.

            S Offline
            S Offline
            sofiane
            wrote on last edited by
            #4

            @SGaist It worked when I removed the qrc/, I wonder why, I always worked with qrc:/filename.png, now it required only :filename.png?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              No, it's always been ":/". AFAR, qrc:/ is used in QML and when using QUrl.

              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
              1
              • S Offline
                S Offline
                sofiane
                wrote on last edited by
                #6

                @SGaist sorry for reopening the issue, I wanted to add a picture to a pre-existing QWidget object, is it possible to do that ? this method requires creating another class which inherits from qwidget, and implement the paintEvent, but I just want to add a picture to an already existing qwidget window.

                1 Reply Last reply
                -1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  QLabel

                  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

                  • Login

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