Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何将rgb转换为uint索引或rgb_Qt - Fatal编程技术网

Qt 如何将rgb转换为uint索引或rgb

Qt 如何将rgb转换为uint索引或rgb,qt,Qt,我正在使用 QImage::setPixel(x, y, uint index_or_rgb); // http://qt-project.org/doc/qt-4.8/qimage.html#setPixel-2 但我不知道如何将rgb值转换为uint。如何将rgb值转换为uint QColor *c = new QColor(185, 20, 120); image.setPixel(i, i2, c->value()); // this doesnt work 1.请参阅QIma

我正在使用

QImage::setPixel(x, y, uint index_or_rgb); // http://qt-project.org/doc/qt-4.8/qimage.html#setPixel-2
但我不知道如何将rgb值转换为uint。如何将rgb值转换为uint

QColor *c = new QColor(185, 20, 120);
image.setPixel(i, i2, c->value()); // this doesnt work

1.
请参阅
QImage::setPixel
的文档:

如果图像的格式为单色或8位,则给定的索引_或_rgb值必须是图像颜色表中的索引,否则该参数必须是QRgb值

我假设您的图像既不是单色的,也不是8位的,因此您需要获得
QRgb
值(它是
unsigned int
的typedef)

2.
接下来,如果您查看
QColor
文档,您会注意到
QColor
中的
QRgb
值应该使用
rgb()
(或者
rgba()
如果您在图像中使用alpha通道)

3.
请注意,不应使用
new
创建
QColor
。它通常在堆栈上创建并按值传递。因此,您的代码应更正如下:

QColor c(185, 20, 120);
image.setPixel(i, i2, c.rgba());

还要注意,
value()
不会返回整个
QColor
的表示形式。它只返回HSV表示的
QColor
的三个组件之一。似乎
value()
与您的用例完全无关。

这种方法可能有效,请尝试一下

setPixel(x, y, qRgb(185, 20, 120));