[Qt-interest] native painting in a QWidget on Windows

Malyushytsky, Alex alex at wai.com
Wed Dec 29 22:13:37 CET 2010


You could try to play with widget attributes.
Start with setAttribute(Qt::WA_NativeWindow);

I've never done mixed painting, but one of my application
allows another application to do native painting in window it owns.

This also means that it had to work outside of paint event.
And it works, even though I had to change flags/attributes a few times
when upgrading Qt 4 to the next minor version.

Alex


From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of John Weeks
Sent: Wednesday, December 29, 2010 12:24 PM
To: Qt-interest Interest
Subject: Re: [Qt-interest] native painting in a QWidget on Windows

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



---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”


More information about the Qt-interest-old mailing list