【发布时间】:2013-06-18 15:57:08
【问题描述】:
我创建了一个格式为QImage::Format_Mono 的QImage。当我尝试通过QGraphicsScene 将图像显示到QGraphicsView 时,视图没有改变。使用通过QPixmap::fromImage() 函数生成的QPixmap 将QImage 加载到场景中。我还尝试使用保存功能将QPixmap 保存为PNG/JPG/BMP,但无济于事。基本代码结构如下:
QGraphicsView *view = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
view.setScene(scene);
QImage img(size,QImage::Format_Mono);
QVector<QRgb> v;
v.append(Qt::color0); // I have tried using black and white
v.append(Qt::color1); // instead of color0 and 1 as well.
img.setColorTable(v);
// Do some stuff to populate the image using img.setPixel(c,r,bw)
// where bw is an index either 0 or 1 and c and r are within bounds
QPixmap p = QPixmap::fromImage(img);
p.save("mono.png");
scene->addPixmap(p);
// Code to display the view
如果我改为制作QImage::Format_RGB888 的图像并用黑色或白色填充像素,PNG/View 会正确显示。
如何更新我的代码以在QGraphicsView 中显示QImage?
【问题讨论】:
-
顺便说一句,我猜
view.setScene(scene);只是 SO 中的一个错字,而不是你的代码。
标签: c++ qt qgraphicsview qgraphicsscene qimage