Is there a method in Qt with which one can easily create a picture based on data stored in a std::vector
? I mean that in the vector there are colors for each QPointF
points of a QWidget
on which I'm painting with QPainter
, but I don't only need to draw this picture on the QWidget
using the colors in the vector, but to save it as a picture too.
在Qt中是否有一个方法可以根据存储在std :: vector中的数据轻松创建图片?我的意思是在向量中有QWidget的每个QPointF点的颜色,我用QPainter绘制,但是我不仅需要使用向量中的颜色在QWidget上绘制这个图片,而是要保存它作为一张照片。
1 个解决方案
#1
If you know the initial dimensions of your image and have a vector with the color information, you can do the following:
如果您知道图像的初始尺寸并且具有带颜色信息的矢量,则可以执行以下操作:
// Image dimensions.
const int width = 2;
const int height = 2;
// Color information: red, green, blue, black pixels
unsigned int colorArray[width * height] =
{qRgb(255, 0, 0), qRgb(0, 255, 0), qRgb(0, 0, 255), qRgb(0, 0, 0)};
// Initialize the vector
std::vector<unsigned int> colors(colorArray, colorArray + width * height);
// Create new image with the same dimensions.
QImage img(width, height, QImage::Format_ARGB32);
// Set the pixel colors from the vector.
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
img.setPixel(row, col, colors[row * width + col]);
}
}
// Save the resulting image.
img.save("test.png");
#1
If you know the initial dimensions of your image and have a vector with the color information, you can do the following:
如果您知道图像的初始尺寸并且具有带颜色信息的矢量,则可以执行以下操作:
// Image dimensions.
const int width = 2;
const int height = 2;
// Color information: red, green, blue, black pixels
unsigned int colorArray[width * height] =
{qRgb(255, 0, 0), qRgb(0, 255, 0), qRgb(0, 0, 255), qRgb(0, 0, 0)};
// Initialize the vector
std::vector<unsigned int> colors(colorArray, colorArray + width * height);
// Create new image with the same dimensions.
QImage img(width, height, QImage::Format_ARGB32);
// Set the pixel colors from the vector.
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
img.setPixel(row, col, colors[row * width + col]);
}
}
// Save the resulting image.
img.save("test.png");