[Qt-interest] native painting in a QWidget on Windows
John Weeks
john at wavemetrics.com
Wed Dec 29 21:24:07 CET 2010
I must ask hard questions :)
In thinking about my problem (in the absence of a better solution) it seemed like perhaps I could create a memory device context (a bitmap context) and do my drawing into that. When the drawing is done, convert it to a QPixMap (presumably using QPixmap::fromWinHBITMAP) and then draw the QPixMap into my widget using QPainter::drawPixMap().
Has anyone done anything like this?
Will this result in poor performance? It seems like there is a possibility that the conversion+extra draw might take some time.
I'd be really interested in any reaction you folks might have to this idea, or a real solution to the problem outlined below.
Thanks!
John Weeks
WaveMetrics, Inc.
Phone (503) 620-3001
Fax (503) 620-6754
email support at WaveMetrics.com
On Dec 27, 2010, at 2:54 PM, John Weeks wrote:
> I have a need to use native GDI+ painting inside a QWidget. I wish I didn't, but can't get around it yet. I would be very grateful for any help with this.
>
> I am able to paint using Core Graphics on Macintosh, but using the same code with GDI+ doesn't work. It appears that the QPainter calls are not drawing to the screen- if I step through my code in the debugger, I see my GDI+ drawing appear as a step over it. But the QPainter calls that came before don't paint until after paintEvent() returns.
>
> I found QPainter::beginNativePainting(); the description sounds good, but the examples I can find seem to indicate that it is only for use with OpenGL.
>
> I also found the Qt::WA_PaintOnScreen attribute, but that doesn't seem to fix my problem.
>
> Here is my code, called from paintEvent(). It has an accumulation of attempted fixes in it...
>
> void IgorTestContent::doPaint()
> {
> QWidget * widget = windowPtr->getContentWidget();
> QRect r = widget->geometry();
> QPainter painter(widget);
>
> // do some QPainter painting just to make sure that I can...
>
> QLinearGradient titleGradient(r.width()/2, 0, r.width()/2, r.height());
> titleGradient.setColorAt(0, QColor(255, 0, 0));
> titleGradient.setColorAt(1, QColor(255, 255, 0));
> QBrush gradientBrush(titleGradient);
> painter.setBrush(gradientBrush);
>
> painter.drawRect(0, 0, r.width()-1, r.height()-1);
>
> painter.drawLine(r.x()+10, r.y()+10, r.x()+r.width()-10, r.y()+r.height()-10);
>
> painter.drawText(r.x()+10, r.y()+r.height()/2, "This text was drawn by QPainter.");
>
> // Try native painting. A Core Graphics version of this code works on Macintosh.
> painter.beginNativePainting();
> void * hdc = (void *)widget->getDC(); // void * so I don't have to have Windows API's here. Will be replaced eventually with a cross-platform version.
> doNativePaint(hdc); // this function encapsulates native painting.
> widget->releaseDC((HDC)hdc); // Again, will be replaced eventually with a cross-platform version.
> painter.endNativePainting();
> }
>
>
> Regards,
> John Weeks
>
> WaveMetrics, Inc.
> Phone (503) 620-3001
> Fax (503) 620-6754
> email support at WaveMetrics.com
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101229/6f21945e/attachment.html
More information about the Qt-interest-old
mailing list