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.

        ui->setupUi(this);
    
    
    
    
       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 );
    
       ui->graphicsView->show();
    

  • Qt Champions 2016

    Hi
    Did you read
    http://doc.qt.io/qt-5/graphicsview.html
    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

    Hi,

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.