Qt6 topics merged with General

Draw image to a qgraphicsview

  • Hi All,
    I'm trying to work out how to draw an image to a graphics view but am having no luck. What I've done is use creator to produce a dialog with a graphicsview attached. In the constructor for this I try to test by by loading an image and just drawimg to the graphics view. I create a scene ( not really sure why I need to do this? But am trying to copy some on line ideas. Then try to show. Debugging shows the bitmap is loaded, but nothing is draw. Can anyone advise please.

       QImage image("/home/tjh/Desktop/test/test.png");
       QGraphicsPixmapItem item( QPixmap::fromImage( image ) );
       QGraphicsScene* scene = new QGraphicsScene;
       ui->graphicsView->setScene( scene );
       scene->addItem( &item );
       item.setPos( 0, 0 );

  • Lifetime Qt Champion

    Did you read
    You need the scene as its the holder of all the objects.

    Im not sure the reason you do not see the QGraphicsPixmapItem.
    You are placing it at 0,0. are you sure that location is showing in the view?

    Maybe use the 40000 sample as playing ground?
    It can easy move around view. etc.

  • Lifetime Qt Champion


    You are allocating your QGraphicsPixmapItem on the stack in the constructor so it will get destroyed at the end of it thus won't be visible when the scene is finally shown.

Log in to reply