Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use a png image as a QGraphicItem?
Qt 6.12 LTS is out! See what's new in the release blog

How to use a png image as a QGraphicItem?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.5k 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.
  • F Offline
    F Offline
    frnklu20
    wrote on last edited by
    #1

    How can I do this?I don't want to use an allipse or a circle or a rect

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      what about
      https://doc.qt.io/qt-5/qgraphicspixmapitem.html
      that would load png.

      1 Reply Last reply
      1
      • F Offline
        F Offline
        frnklu20
        wrote on last edited by frnklu20
        #3

        Oh yeah! But it gives me an error

        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("C:/Users/ILHA4/Desktop/trafo"), &scene);
        

        It says:
        "no matching constructor for initialization of 'QGraphicsPixmapItem'"

        UPDATE:

        dialog.h

        #include <QGraphicsPixmapItem>
        
        namespace Ui {
        class Dialog;
        }
        
        class Dialog : public QDialog
        {
        ...
                QGraphicsPixmapItem *item;
        };
        

        dialog.cpp

        #include <QGraphicsPixmapItem>
        
        Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {
            ui->setupUi(this);
        
            scene= new QGraphicsScene(this);
            ui->graphicsView->setScene(scene);
            QBrush redBrush(Qt::red);
            QPen blackpen(Qt::black);
            
            item->setPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo"));
            scene->addItem(item);
            ellipse=scene->addEllipse(10,10,100,100,blackpen,redBrush);
            ellipse->setFlag(QGraphicsItem::ItemIsMovable);
        }
        

        When i do this the program unexpectedly finishes :(

        UPDATE 2:

        dialog.cpp

        Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {...
        
              QGraphicsPixmapItem *item=scene->addPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo"));
            item->setPos(50,50);
            scene->addItem(item);
        }
        

        It doesn't give me an error, but the item doesn't appear on the screen :(

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

          Hi,

          @frnklu20 said in How to use a png image as a QGraphicItem?:

          "C:/Users/ILHA4/Desktop/trafo"

          That's pretty unusual for an image name. The extension is missing. Is that on purpose ?

          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
          • F Offline
            F Offline
            frnklu20
            wrote on last edited by
            #5

            thats the path of the image, where it is located on my computer

            the name of the image is trafo, of high voltage transformer.

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

              What type of image is that ? Again, it's really unusual for an image file to not have an extension.

              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
              • F Offline
                F Offline
                frnklu20
                wrote on last edited by
                #7

                it's a png image, but if i do:

                GraphicsPixmapItem *item=scene->addPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo.png"));
                

                it doesn't work either, the image doesn't appear on the screen

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

                  It that case, check whether the QPixmap is null.

                  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
                  • F Offline
                    F Offline
                    frnklu20
                    wrote on last edited by
                    #9

                    Yes that's it, the path was wrong

                    Thanks man, you're awesome!

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