QImage in a QGraphics scene

柔情痞子 提交于 2019-12-20 10:35:19

问题


I am quite new to Qt. I am having troubles in inserting a QImage to a scene. Could somebody please tell me how to add a QImage to a QGraphicsScene?


回答1:


For this you would use a QGraphicsPixmapItem that you add to the scene like any other QGraphicsItem.

The QGraphicsPixmapItem can be initialized with a QPixmap which is a device-dependent representation of a bitmap and you can get it from a QImage for example with the static function QPixmap::fromImage().

Update (example code)

#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();
}


来源:https://stackoverflow.com/questions/5960074/qimage-in-a-qgraphics-scene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!