C++ 填充(Qt::红色)是否始终为黑色?

C++ 填充(Qt::红色)是否始终为黑色?,c++,qt,user-interface,colors,qimage,C++,Qt,User Interface,Colors,Qimage,我正试图弄清楚QImage是如何工作的。对于开始,我只想创建一个400x400像素的图像,并尝试将其填充为红色。好吧,法师是满的,但是黑的。。。 我还想创建一个单色图像。一种颜色应该是透明的,另一种颜色应该是任何其他颜色(例如:红色)。我该怎么做?我用setcolor试过了,但似乎不起作用 scene = new QGraphicsScene(this); ui.graphicsView->setScene(scene); QImage *image = new QImage(400, 4

我正试图弄清楚QImage是如何工作的。对于开始,我只想创建一个400x400像素的图像,并尝试将其填充为红色。好吧,法师是满的,但是黑的。。。 我还想创建一个单色图像。一种颜色应该是透明的,另一种颜色应该是任何其他颜色(例如:红色)。我该怎么做?我用setcolor试过了,但似乎不起作用

scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(400, 400, QImage::Format_Indexed8); //QImage::Format_Mono);
image->fill(Qt::red);
//image->setColor(1, Qt::transparent);
//image->setColor(0, Qt::red);
scene->addPixmap(QPixmap::fromImage(*image));

原因是传递给构造函数的
QImage::Format
。使用例如
QImage::Format_RGB32
获得接受颜色的图像

要使用图像格式,需要使用
setColor
方法,如8位案例所示

QImage image(3, 3, QImage::Format_Indexed8);
QRgb value;

value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);

value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);

value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);

image.setPixel(0, 1, 0);
image.setPixel(1, 0, 0);
image.setPixel(1, 1, 2);
image.setPixel(2, 1, 1);
导致

简短回答: 在
QImage
构造函数中使用
Format\u RGB32
而不是
Format\u Indexed8

详细答复:
Format_Indexed8
使用手动定义的颜色表,其中每个索引表示一种颜色。您必须为图像创建自己的颜色表:

QVector<QRgb> color_table;
for (int i = 0; i < 256; ++i) {
    color_table.push_back(qRgb(i, i, i)); // Fill the color table with B&W shades
}
image->setColorTable(color_table);
如您所见,
Format_Indexed8
像素值表示的不是RGB颜色,而是索引值(依次表示在颜色表中设置的颜色)

Format\u Mono
是另一种也使用颜色表的格式(请注意,其中只允许使用两种颜色)

补充答复: 一种颜色应该是透明的,另一种颜色应该是任何其他颜色(例如:红色)

如果我正确理解您的意思,此代码将执行您想要的操作:

// Create a 256x256 bicolor image which will use the indexed color table:

QImage *image = new QImage(256, 256, QImage::Format_Mono);

// Manually set our colors for the color table:

image->setColorCount(2);
image->setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
image->setColor(1, qRgba(0, 0, 0, 0));     // Index #1 = Transparent

// Testing - Fill the image with pixels:

for (short x = 0; x < 256; ++x) {
    for (short y = 0; y < 256; ++y) {
        if (y < 128) {
            // Fill the part of the image with red color (#0)
            image->setPixel(x, y, 0);
        }
        else {
            // Fill the part of the image with transparent color (#1)
            image->setPixel(x, y, 1);
        }
    }
}
//创建一个256x256双色图像,该图像将使用索引颜色表:
QImage*image=新的QImage(256,256,QImage::Format_Mono);
//手动设置颜色表的颜色:
图像->设置颜色计数(2);
图像->设置颜色(0,qRgba(255,0,0,255));//索引#0=红色
图像->设置颜色(1,qRgba(0,0,0,0));//索引#1=透明
//测试-用像素填充图像:
用于(短x=0;x<256;++x){
用于(短y=0;y<256;++y){
if(y<128){
//用红色(#0)填充图像部分
图像->设置像素(x,y,0);
}
否则{
//用透明颜色(#1)填充图像部分
图像->设置像素(x,y,1);
}
}
}

如for QImage:警告中所述:不支持使用格式QImage::format_Indexed8在QImage上绘制。Format_Indexed8选项使用颜色表的索引,其中表是Qvector如果您尝试
QImage(400400,QImage::Format_ARGB32),会怎么样?@vahancho:y你没有阅读答案吗?谢谢其他格式,它是红色的!但是,是否可以将单色图像中的颜色更改为例如红色和透明而不是黑白?你的意思是,你希望红色图像是透明的?不,单色图像将具有黑色和白色。我希望我的图像具有红色和透明的颜色。只需使用ARGB格式,并将图像的某些部分设置为完全透明即可完成此工作?我不确定,它是真的透明还是只是一个错误使它看起来是透明的`QImage*image=新的QImage(400400,QImage::Format_Mono);图像->设置颜色计数(2);图像->设置颜色(0,qRgb(255,0,0));图像->设置颜色(1,Qt::透明);图像->填充(1)`非常感谢。但是如何使用
格式单声道
?我要的不是黑白的单色图像,而是红色和透明的。这样行吗?我不确定,它是真的透明还是只是一个错误使它看起来是透明的`QImage*image=新的QImage(400400,QImage::Format_Mono);图像->设置颜色计数(2);图像->设置颜色(0,qRgb(255,0,0));图像->设置颜色(1,Qt::透明);图像->填充(1)`@honiahaka10查看更新的答案。实际上,我不确定
Format\u Mono
是否使用颜色表,但
Format\u Indexed8
肯定会使用。“用于操纵图像像素的函数取决于图像格式。原因是单色和8位图像是基于索引的,并且使用颜色查找表。”文档说是这样,所以我认为它应该可以工作。谢谢大家!@honiahaka10+1用于读取文档。考虑到你的评论,我改进了我的回答。
// Create a 256x256 bicolor image which will use the indexed color table:

QImage *image = new QImage(256, 256, QImage::Format_Mono);

// Manually set our colors for the color table:

image->setColorCount(2);
image->setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
image->setColor(1, qRgba(0, 0, 0, 0));     // Index #1 = Transparent

// Testing - Fill the image with pixels:

for (short x = 0; x < 256; ++x) {
    for (short y = 0; y < 256; ++y) {
        if (y < 128) {
            // Fill the part of the image with red color (#0)
            image->setPixel(x, y, 0);
        }
        else {
            // Fill the part of the image with transparent color (#1)
            image->setPixel(x, y, 1);
        }
    }
}