Put an item on the QGraphicsView



  • Hi,

    I've created via the designer a form with a qgraphicsview in it. Now in my constructor of the mainwindow I would like to place a image in the view, but nothing works. I thought I could do it with ui->nameofview->addPixmapItem but I can't?

    It's the first time it do this via the designer so what is wrong with?

    In my previous project I did it without the creator with this code:

    @
    item = new QGraphicsPixmapItem( QPixmap::fromImage(worldImage));
    this->addItem(item);
    @

    but this doens't work :s

    Kind regards,



  • Is the image linked well? I've seen a lot of threads where the filepaths aren't formed well.



  • Hi,
    You need to add a scene to your graphics view and then add the QGraphicsPixmapItem to the scene
    e.g

    @scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    QImage worldImage("your image path");
    item = scene->addPixmap(QPixmap::fromImage(worldImage));@

    Where item is QGraphicsPixmapItem and scene is QGraphicsScene.

    Try If this works.



  • Jup forgot this because of the creator :-) thanks.



  • Yes this is very common mistake, even i got confused initially when using the graphicsView. Kindly Edit your first Post and mark it [SOLVED],

    Happy Coding !!!!


Log in to reply
 

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