在Qt5中更改QGraphicsScene/View中的Pixmap位置

Change Pixmap Position in QGraphicsScene/View in Qt5

本文关键字:View 中的 Pixmap 位置 QGraphicsScene Qt5      更新时间:2023-10-16

我有一个普通的QGraphicsView/QGraphicsScene,我想做的就是将QPixmap(.png)加载到图形中,并手动设置该QPixmap图像的位置。我找到了解决方案,但它们在Qt5上不起作用。关于如何在Qt5上实现这一点,有什么想法吗?非常感谢。

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);

您可以从计算机中浏览图像并设置其位置。以防万一,它能帮助别人。

        QString fileNamez = QFileDialog::getOpenFileName(this,"Open Filezzz","C:/");
         QGraphicsPixmapItem *pm = scene->addPixmap( QPixmap(fileNamez) );
         pm->setPos(xPos,yPos);

如果在实现中使用QGraphicsPixmapItem,或者实际上使用任何Qt对象,则需要将其包含在cpp:-中

#include <QGraphicsPixmapItem>