2

I have a normal QGraphicsView/QGraphicsScene and all I want to do is load a QPixmap (.png) to the graphic and manually set the position of that QPixmap image. I've found solutions but they don't work on Qt5. Any ideas on how to achieve this on Qt5? Thank you.

QGraphicsScene* scene = new QGraphicsScene(QRect(0, 0, 600, 400));

QPixmap Pix(":/test.gif");

// doesnt work
//Pix.setGeometry(QRect(-30, 40, 260, 200));


QGraphicsPixmapItem *item1 = scene->addPixmap(Pix);

// doesnt work
//item1->setPos(-25, 45);
//scene->addPixmap(Pix)->setPos(0,0);

//QGraphicsView* view = new QGraphicsView(ui->centralWidget);

QGraphicsView* view = new QGraphicsView(this);

view->setScene(scene);
  • What do you mean by "they don't work"? Do you see the pixmap and it's not being positioned, or are you not seeing anything? – TheDarkKnight Oct 07 '14 at 13:49

2 Answers2

1

You can browse the image from your computer and set its position. Just in case, It helps someone.

        QString fileNamez = QFileDialog::getOpenFileName(this,"Open Filezzz","C:/");
         QGraphicsPixmapItem *pm = scene->addPixmap( QPixmap(fileNamez) );
         pm->setPos(xPos,yPos);
Shachi
  • 1,858
  • 3
  • 23
  • 41
0

If you use a QGraphicsPixmapItem in your implementation, or indeed any Qt object, you need to include it in the cpp: -

#include <QGraphicsPixmapItem>
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • 1
    Please accept the most helpful / correct answer by clicking the tick left to the answer, thank you :) – dom0 Oct 07 '14 at 14:25