C++ QImage::将单声道格式化为.png和Qgraphicscene

C++ QImage::将单声道格式化为.png和Qgraphicscene,c++,qt,qgraphicsview,qgraphicsscene,qimage,C++,Qt,Qgraphicsview,Qgraphicsscene,Qimage,我已经创建了一个格式为QImage::format_Mono的QImage。当我尝试通过qgraphicscene将图像显示到QGraphicsView时,视图保持不变。使用通过QPixmap::fromImage()函数生成的QPixmap将QImage加载到场景中。我还尝试使用save函数将QPixmap保存为PNG/JPG/BMP,但没有成功。基本代码结构如下所示: QGraphicsView *view = new QGraphicsView(); QGraphicsScene *sce

我已经创建了一个格式为
QImage::format_Mono
QImage
。当我尝试通过
qgraphicscene
将图像显示到
QGraphicsView
时,视图保持不变。使用通过
QPixmap::fromImage()
函数生成的
QPixmap
QImage
加载到场景中。我还尝试使用save函数将
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
QGraphicsView*view=newqgraphicsview();
qgraphicscene*场景=新的qgraphicscene();
视图.场景(场景);
QImage img(大小,QImage::格式);
qv矢量;
v、 追加(Qt::color0);//我试过用黑白两色
v、 附加(Qt::color1);//而不是颜色0和1。
img.可设置颜色(v);
//使用img.setPixel(c、r、bw)填充图像
//其中,bw是一个索引0或1,c和r在范围内
QPixmap p=QPixmap::fromImage(img);
p、 保存(“mono.png”);
场景->添加pixmap(p);
//显示视图的代码
如果我将图像改为
QImage::Format_RGB888
,并用黑色或白色填充像素,则PNG/视图将适当显示


如何更新代码以在
QGraphicsView
中显示
QImage

错误在于
Qt::GlobalColor
s(如
Qt::white
Qt::color0
)的类型为
QColor
,而不是
QRgb
。(
QRgb
是无符号int的typedef)

您可以使用方法
QColor::rgb()
QColor
转换为
QRgb
,或者使用全局方法
QRgb(r,g,b)
直接创建
QRgb
。下面是一个完整的工作示例,它显示(并保存为PNG)非常精确的图像,无论
mono
true
还是
false

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QGraphicsView *view = new QGraphicsView();
    QGraphicsScene *scene = new QGraphicsScene();
    view->setScene(scene);

    int W = 100;
    int H = 100;
    QImage img;
    uint color0 = qRgb(255,0,0);
    uint color1 = Qt::green.rgb();
    bool mono = true;
    if(mono)
    {
        img = QImage(QSize(W,H),QImage::Format_Mono);
        QVector<QRgb> v; v << color0 << color1;
        img.setColorTable(v);
        for(int i=0; i<W; i++)
        for(int j=0; j<H; j++)
        {
            uint index;
            if(j-(j/10)*10 > 5)
                index = 0;
            else
                index = 1;
            img.setPixel(i,j,index);
        }
    }
    else
    {
        img = QImage(QSize(W,H),QImage::Format_RGB888);
        for(int i=0; i<W; i++)
        for(int j=0; j<H; j++)
        {
            uint color;
            if(j-(j/10)*10 > 5)
                color = color0;
            else
                color = color1;
            img.setPixel(i,j,color);
        }
    }

    QPixmap p = QPixmap::fromImage(img);
    p.save("mono.png");
    scene->addPixmap(p);
    view->show();

    return app.exec();
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
QApplication应用程序(argc、argv);
QGraphicsView*视图=新的QGraphicsView();
qgraphicscene*场景=新的qgraphicscene();
查看->设置场景(场景);
int W=100;
int H=100;
QImage img;
uint color0=qRgb(255,0,0);
uint color1=Qt::green.rgb();
布尔单声道=真;
中频(单声道)
{
img=QImage(QSize(W,H),QImage::Format_Mono);

QVector v;v顺便说一句,我想
view.setScene(scene);
只是SO中的一个输入错误,而不是在你的代码中。你是对的。我发现问题在于我使用的是Qt全局颜色,即
Qt::white
Qt::color0
。我必须使用
QColor(Qt::white).rgb()
转换这些值,使其按预期运行。