我创建了一个QImage
格式QImage::Format_Mono
。当我尝试QGraphicsView
通过 a将图像显示到 a 时QGraphicsScene
,视图保持不变。使用通过函数生成的QImage
a 加载到场景中。我还尝试使用保存功能将其保存为 PNG/JPG/BMP,但无济于事。基本代码结构如下:QPixmap
QPixmap::fromImage()
QPixmap
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/视图会正确显示。
如何更新我的代码以显示QImage
in a QGraphicsView
?