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

John Weeks john at wavemetrics.com
Mon Dec 27 23:54:48 CET 2010


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101227/b9397966/attachment.html 


More information about the Qt-interest-old mailing list