15

我对 Qt 很陌生。我在将 a 插入QImage场景时遇到了麻烦。有人可以告诉我如何将 a 添加QImage到 aQGraphicsScene吗?

4

2 回答 2

19

为此,您将使用添加到场景中QGraphicsPixmapItem的 a ,就像其他任何.QGraphicsItem

QGraphicsPixmapItem可以使用 a 初始化,它QPixmap是位图的设备相关表示,您可以从 aQImage例如使用静态函数获取它QPixmap::fromImage()

更新(示例代码)

#include <QtGlobal>

#if QT_VERSION >= 0x050000
    #include <QtWidgets>
#else
    #include <QtGui>
#endif

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QImage image("test.png");

    QGraphicsPixmapItem item( QPixmap::fromImage(image));
    QGraphicsScene* scene = new QGraphicsScene;
    scene->addItem(&item);

    QGraphicsView view(scene);
    view.show();
    return a.exec();
}
于 2011-05-11T08:56:56.637 回答
2

正如@Neal 所建议的那样, addPixmap 的工作要简单得多,并且 QGraphicsPixmapItem 由于某些未知原因对我不起作用。假设您有一个QGraphicsScene,QGraphicView和其余程序设置下面的代码将只显示图像:

QString filename = "C:/image.png";
QImage image(fileName);
scene->addPixmap( QPixmap::fromImage(image)); 
于 2020-01-04T12:31:26.080 回答