开发者

QPixmap from QBitmap

开发者 https://www.devze.com 2022-12-20 03:13 出处:网络
I am converting a win32 api graphing library to use the qt library instead. I have managed to compile succesfully on XP, now to test true cross-platform capability of the new and improved graphing lib

I am converting a win32 api graphing library to use the qt library instead. I have managed to compile succesfully on XP, now to test true cross-platform capability of the new and improved graphing library, I am attempting to build it on Ubuntu (9.10).

I have encountered a couple of compile time erros - because some of the classes are Windows specific, so I need to modify the code again.

I have a section of code which looks like this:

//original code (compiled succesfully under Windows)
unsigned short fill[4];
fill[0] = (unsigned short)(pattern & 0xF000) >> 8;
fill[1] = (unsigned short)(pattern & 0x0开发者_运维百科F00) >> 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);


QBitmap is a QPixmap, it inherits this class.


QBitmap is a 1-bit per pixel image and is derived from QPixMap. In other words you can pass it directly to brush1. No need to do anything more with it (other than fill it correctly) ie:

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

This should work just as well under windows as under any other platform ...

Edit: Its worth noting that in both your cases above there is no point in having 4 shorts. 1 short is 16-bits. 4 x 4 1 bit pixels is 16-bits so it only reads the first short anyway ....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号