Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, December 2006
QPainter and widget painting error


Message 1 in thread

I'm trying to build a sample app to demonstrate a different problem  
that I think may be a bug in Qt or how I'm using Qt.

I have implemented a class derived from QWidget. I'm overriding  
paintEvent() to paint some different colors.

void CustomView::paintEvent(QPaintEvent *)
{
	QPainter p(this);
	++color;
	switch(color) {
		case 1:
			p.fillRect(rect(), QBrush(Qt::green));
			break;
		case 2:
			p.fillRect(rect(), QBrush(Qt::red));
			break;
		case 3:
			p.fillRect(rect(), QBrush(Qt::blue));
			break;
		case 4:
		default:
			p.fillRect(rect(), QBrush(Qt::cyan));
			color = 0;
			break;
	}
	
}

When this method gets called and the QPainter is created, the message  
"QPainter::begin: Widget painting can only begin as a result of a  
paintEvent". However, it doesn't ALWAYS do this. It paints first time  
OK and if I resize the window, it repaints OK.

What I'm trying to do is to have a separate thread that is calling  
qApp->postEvent(myCustomView, new QPaintEvent(myCustomView->rect()).  
This thread just loops, posts a paint event, then calls usleep() for  
1/2 second. The error only happens due to paint events that were  
posted in this manner.

What I don't get is why it complains that I'm drawing a widget from  
outside of paintEvent when I'm actually responding to a paint event.

This has got to be something stupid. Any ideas what I'm doing wrong?

--
 [ signature omitted ] 

Message 2 in thread

Brant,

This came up during the Qt 4 Beta cycle on the feedback list.  The strange thing about the only paint inside paintevent rule is that the event also has to be a "spontaneous" event.  You might be better off doing a cross thread sig/slot to update(rect) although that might cost you two laps through the event loop.

--Justin


-----Original Message-----

From:  Brant Sears <brant@xxxxxxx>
Subj:  QPainter and widget painting error
Date:  Thu Nov 30, 2006 6:58 pm
Size:  1K
To:  QT Interest List <qt-interest@xxxxxxxxxxxxx>

I'm trying to build a sample app to demonstrate a different problem  
that I think may be a bug in Qt or how I'm using Qt.

I have implemented a class derived from QWidget. I'm overriding  
paintEvent() to paint some different colors.

void CustomView::paintEvent(QPaintEvent *)
{
	QPainter p(this);
	++color;
	switch(color) {
		case 1:
			p.fillRect(rect(), QBrush(Qt::green));
			break;
		case 2:
			p.fillRect(rect(), QBrush(Qt::red));
			break;
		case 3:
			p.fillRect(rect(), QBrush(Qt::blue));
			break;
		case 4:
		default:
			p.fillRect(rect(), QBrush(Qt::cyan));
			color = 0;
			break;
	}
	
}

When this method gets called and the QPainter is created, the message  
"QPainter::begin: Widget painting can only begin as a result of a  
paintEvent". However, it doesn't ALWAYS do this. It paints first time  
OK and if I resize the window, it repaints OK.

What I'm trying to do is to have a separate thread that is calling  
qApp->postEvent(myCustomView, new QPaintEvent(myCustomView->rect()).  
This thread just loops, posts a paint event, then calls usleep() for  
1/2 second. The error only happens due to paint events that were  
posted in this manner.

What I don't get is why it complains that I'm drawing a widget from  
outside of paintEvent when I'm actually responding to a paint event.

This has got to be something stupid. Any ideas what I'm doing wrong?

--
 [ signature omitted ]