Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
QPixmap::toWinHBITMAP
I'm trying to paint to a QPixmap using GDI methods but I'm encountering some difficulties:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
m_pmOffscreen = new QPixmap(550, 400);
m_bmpOffscreen = (HBITMAP) m_pmOffscreen->toWinHBITMAP();
m_dcOffscreen = CreateCompatibleDC( GetDC(NULL) );
HGDIOBJ hPrevSel= SelectObject( m_dcOffscreen, m_bmpOffscreen );
//------------ check the size of the returned bitmap
//------------ PROBLEM 1: returns {0,0} in dimension,
//------------ where { 550, 400 } is expected.
SIZE dimension = {0,0};
bool bBDE = GetBitmapDimensionEx( m_bmpOffscreen, &dimension );
//------------ results in a green background, as expected.
m_pmOffscreen->fill( 0x00FF00 );
//------------ PROBLEM 2: text doesn't end up in the pixmap:
QString hello = "hello world!";
bool bResult = TextOut
( m_dcOffscreen
, 100, 100
, hello.toStdWString().data()
, 12
);
|
I guess the TextOut doesn't show because the dimensions on the returned HBITMAP that is selected in the DC, are wrong? Am I retreiving the HBITMAP the wrong way? Any help is appriciated!
Cheers,
Ryko
RE: QPixmap::toWinHBITMAP
Wouldn't it be easier to use QPainter?
I have enough sense to know that "common sense" is an oxymoron.
RE: QPixmap::toWinHBITMAP
Absolutely, but the code snippet is just an abstract for a Netscape plugin my 'real' code is hosting that will only paint to a HDC. QPainter won't solve that, right?
Apart from that, shouldn't the TextOut result in the Pixmap's bits being set? Or am I making a silly mistake?
This post has been edited 1 times, last edit by "Ryko" (Sep 12th 2005, 11:53pm)
RE: QPixmap::toWinHBITMAP
Originally posted by Ryko
Apart from that, shouldn't the TextOut result in the Pixmap's bits being set? Or am I making a silly mistake?
QPixmap doesn't use image bits directly. Don't treat it as an image you can draw on. If you use QPixmaps, why can't you use other Qt functions? Maybe you should use a QImage instead of QPixmap?
RE: QPixmap::toWinHBITMAP
I've tried doing it with a QImage, but I didn't find any methods that will get me a HDC to a QImage. Is there a way to get a HDC to paint to a QImage?
As a workaround, I've tried QPixmap::fromImage and then getting a HDC on the resulting pixmap, but to no avail (since as you just explained QPixmap doesn't use the HBITMAP bits directly?)
RE: QPixmap::toWinHBITMAP
The thing is probably that toWinHBITMAP doesn't return a reference to a bitmap, but a copy of it, so you're not drawing on a pixmap but on a structure created from the pixmap. You'd need to call QPixmap::fromWinHBITMAP() after you modify the structure.
RE: QPixmap::toWinHBITMAP
Thanks for all the help sofar. If 'toWinHBITMAP' does indeed return a copy of the bitmap then I'll have to resort to using other techniques: it would be too much of a performance hit, since the plug-in's representation is often updated.
The reason I tried to use a QPixmap is that I wanted to push forward the usage of platform dependent code forward as much as possible. I guess I'd now better write a seperate Mac and Windows methods for the Netscape plug-in to draw on each OS's native surface, and then getting it displayed in a QWidget.
Hopefully, the other way around 'fromWinHBITMAP' will not actually copy all the bits? One should think that pre-pending a DIB header would suffice?
RE: QPixmap::toWinHBITMAP
Why can't you just use platform independent techniques Qt provides?
RE: QPixmap::toWinHBITMAP
Because the Netscape plugin can't draw to them: on a Mac it needs a port to draw to and on windows a HDC.