来自QBitmap的QPixmap

时间:2010-02-16 16:23:47

标签: c++ qt winapi gdi

我正在转换win32 api图形库以改为使用qt库。我已经设法在XP上成功编译,现在测试新的和改进的图形库的真正的跨平台功能,我试图在Ubuntu(9.10)上构建它。

我遇到了几个编译时错误 - 因为有些类是Windows特定的,所以我需要再次修改代码。

我有一段代码如下:

//original code (compiled succesfully under Windows)
unsigned short fill[4];
fill[0] = (unsigned short)(pattern & 0xF000) >> 8;
fill[1] = (unsigned short)(pattern & 0x0F00) >> 4;
fill[2] = (unsigned short)(pattern & 0x00F0);
fill[3] = (unsigned short)(pattern & 0x000F) >> 4;

HBITMAP hbmp = CreateBitmap(4, 4, 1, 1, fill);
QPixmap texture;
texture.fromWinHBITMAP(hbmp);
DeleteObject(hbmp);
brush1.setTexture(texture);


//Code to compile on Ubuntu
unsigned short fill[4];
fill[0] = (unsigned short)(pattern & 0xF000) >> 8;
fill[1] = (unsigned short)(pattern & 0x0F00) >> 4;
fill[2] = (unsigned short)(pattern & 0x00F0);
fill[3] = (unsigned short)(pattern & 0x000F) >> 4;

QBitmap bmp(4, 4, fill);  //Is this correct ?
QPixmap texture;
texture.fromWinHBITMAP(bmp);   // How to convert QBitmap to QPixmap ?
//DeleteObject(hbmp);          // No longer required (local object scope)
brush1.setTexture(texture);

2 个答案:

答案 0 :(得分:2)

QBitmap是每像素1位的图像,源自QPixMap。换句话说,您可以将它直接传递给brush1。无需再做任何事情(除了正确填写),即:

QBitmap bmp = QBitmap::FromData( QSize( 4, 4 ), fill );
brush1.setTexture(bmp);

这在Windows下也应该像在任何其他平台下一样工作......

编辑:值得注意的是,在上述两种情况下,有4条短裤都没有意义。 1短是16位。 4 x 4 1位像素是16位,所以它只能读取第一个短路....

答案 1 :(得分:1)

QBitmap是一个QPixmap,它继承了这个类。