QImage image("example.png"); int newWidth = image.width() + 16 - (image.width() % 16); // Round up width to nearest multiple of 16 int bytesPerLine = newWidth * 4; // RGBA color depth QImage newImage(newWidth, image.height(), QImage::Format_RGBA8888); newImage.fill(Qt::transparent); // Fill with transparent pixel for (int y = 0; y < image.height(); y++) { uchar* dest = newImage.scanLine(y); // Destination scanline uchar* src = image.scanLine(y); // Source scanline memcpy(dest, src, image.bytesPerLine()); // Copy original image }
unsigned char* buffer = getSomeImageBytes(); int imageWidth = 640; int imageHeight = 480; int bytesPerLine = imageWidth * 3; // RGB color depth QImage image(buffer, imageWidth, imageHeight, bytesPerLine, QImage::Format_RGB888);In this example we create a QImage from a buffer that contains the raw image data. We specify the image width and height, and the `bytesPerLine` for the image. We also specify the format of the image data, which in this case is RGB888.