【发布时间】:2014-04-22 06:34:11
【问题描述】:
我想显示一些QImage 到QGraphicsScene,我的代码非常简单:
mainwindow.h
QImage *sourceImage;
QGraphicsView *imageView;
QGraphicsScene *imageScene;
mainwindow.cpp
imageScene = new QGraphicsScene;
imageView = new QGraphicsView;
imageView->setScene(imageScene);
sourceImage = new QImage;
sourceImage.load(":/targetimage.png");
imageScene.addPixmap(QPixmap::fromImage(sourceImage));
然后编译器指出我做错了什么:QGraphicsScene::addPixmap 仅接受 const QPixmap 作为参数,我试图将 QImage 转换为 const QPixmap,这是不允许的,因为 QPixmap::fromImage 仅在接受const QImage,就像const 地狱。
这个方法上的official documentation 对我来说也没有多大意义,例如,如果我想做一个图像查看器,并且在运行时我肯定会将不同的图像加载到QImage sourceImage 中,并且我怎样才能使用const QImage 来做到这一点?
这个问题一直很痛苦,感谢您的建议。此外,如果对 Qt 中的人使用这些方法const 的哲学原因有任何看法,您能否给我一点启发?
【问题讨论】:
-
是什么阻止您使用 QPixmaps 而不是 QImages?
-
我也在使用 opencv,这意味着我会不断地将
cv::Mat转换为QImage并返回。根据文档,QPixmap针对显示进行了优化。我相信有一些方法可以将QPixmap转换为 cv::Mat 但QImage非常适合问题空间。
标签: qt qgraphicsview qgraphicsscene qimage qpixmap