The problem is due to QImage() constructor.
In most cases, QImage(int width, int height,QImage::QFormat format) works fine, however, sometimes the bytesPerLine need to be specified, otherwise this kind of tilted issue happens.
This issue can be solved by the code below:
QImage VTKImageDataToQImage(vtkImageData* imageData)
{
if (!imageData || !imageData->GetPointData() || !imageData->GetPointData()->GetScalars() ||
imageData->GetScalarType() != VTK_UNSIGNED_CHAR)
{
return QImage();
}
int width = imageData->GetDimensions()[0];
int height = imageData->GetDimensions()[1];
vtkIdType numberOfScalarComponents = imageData->GetNumberOfScalarComponents();
QImage image;
QImage::Format format;
if (numberOfScalarComponents == 3)
{
format = QImage::Format_RGB888;
}
else if (numberOfScalarComponents == 4)
{
format = QImage::Format_RGBA8888;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
else if (numberOfScalarComponents == 1)
{
format = QImage::Format_Grayscale8;
}
#endif
else
{
// unsupported pixel format
return QImage();
}
image = QImage(static_cast<uint8_t*>(imageData->GetScalarPointer(0, 0, 0)), width, height,
width * numberOfScalarComponents, format);
return image.mirrored();
}