You are not logged in.

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.

1

Monday, December 19th 2005, 3:42pm

QPainter problem after window refreshment

Hi,
I wrote very basic painting application - when you move your mouse with the button pressed the line is being drawn on the screen.

However I have a problem when the window is refreshed after it was partially hidden (another part was visible all the time). The line which was alerady drawn is refreshed correctly but I can paint only in the area which has been refreshed. The area which was not hidden seems to be "deactivated" (the line which I draw doesn't appear). This "deactivation" is removed when I cover all my window (or minimize it) forcing it be totally refreshed. After that all the lines appear on the screen (even those which were not visible during drawing).

So I wander: Does paintEvent modify something in QPainter instance? Does it change drawing area of my instance pointed by "paint"?

Here is my code:
whiteboard.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "whiteboard.h"
Whiteboard::Whiteboard( QWidget *parent, const char *name ) : QFrame(parent, name)
{
	i_nbLines = 0;
	paint = new QPainter(this);
	pen = new QPen(red, 2);
	paint->setPen(*pen);
}

Whiteboard::~Whiteboard()
{
	delete paint;
}

void Whiteboard::paintEvent(QPaintEvent *)
{
	paint->drawText( rect(), AlignCenter, "The Text" );
	for(int i=0;i<i_nbLines;i++) {
		paint->drawLine(pqt_lineStart[i],pqt_lineEnd[i]);
	}
}

void Whiteboard::mouseMoveEvent(QMouseEvent *e)					// draw line from previous mouse pos to current
{
	qt_currMousePos = e->pos();
	pqt_lineStart[i_nbLines] = qt_prevMousePos;
	pqt_lineEnd[i_nbLines] = qt_currMousePos;
	paint->drawLine(QPoint(0,0),QPoint(100,100));
	paint->drawLine(qt_prevMousePos,qt_currMousePos);
	qt_prevMousePos = qt_currMousePos;
	i_nbLines++;
}

void Whiteboard::mousePressEvent(QMouseEvent *e)				// draw point in current mouse pos
{
	qt_prevMousePos = e->pos();
	pqt_lineStart[i_nbLines] = qt_prevMousePos;
	pqt_lineEnd[i_nbLines] = qt_prevMousePos;
	paint->drawLine(qt_prevMousePos,qt_prevMousePos);
	i_nbLines++;

}


whiteboard.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef WHITEBOARD_H
#define WHITEBOARD_H

#include <qapplication.h>
#include <qpainter.h>
#include <qevent.h>
#include <qpen.h>
#include <qframe.h>


class Whiteboard : public QFrame
{
	Q_OBJECT
public:
	QPoint pqt_lineStart[50000];
	QPoint pqt_lineEnd[50000];
	int i_nbLines;
//	int i_nbPoints;
	QPoint qt_currMousePos;
	QPoint qt_prevMousePos;

	QPainter *paint;
	QPen	 *pen;

	Whiteboard( QWidget *parent=0, const char *name=0 );
	~Whiteboard();
protected:
	void paintEvent(QPaintEvent *);
	void mouseMoveEvent(QMouseEvent *);
	void mousePressEvent(QMouseEvent *);

};

#endif


main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
#include "whiteboard.h"

int main( int argc, char **argv)
{
	QApplication a(argc, argv);
	Whiteboard w;
	w.setGeometry(100,100,800,800);
	a.setMainWidget( &w);
	w.show();
	return a.exec();
}



Thanks in advance

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Monday, December 19th 2005, 5:06pm

RE: QPainter problem after window refreshment

Try creating that QPainter every time you draw something.

3

Friday, December 30th 2005, 4:51pm

RE: QPainter problem after window refreshment [SOLVED]

Thanks.
The problem was caused by using DrawLine method inside QPaintEvent. I have no idea why, but as soon as I removed it the window behaved normally. But without DrawLine I had no refreshing so I used QPixmap and bitBlt instead and it works correctly.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Whiteboard::Whiteboard( QWidget *parent, const char *name ) : QFrame(parent, name)
{
	paint = new QPainter(this);
	pen = new QPen(red, 2);
	qt_pixmap = new QPixmap(this->width(),this->height());
	qt_pixmap->fill(black);
	paint->setPen(*pen);
}

Whiteboard::~Whiteboard()
{
	delete paint;
}

// *********************************************************************************
// Events
// *********************************************************************************
void Whiteboard::paintEvent(QPaintEvent *)
{
	bitBlt(this,0,0,qt_pixmap,0,0);
}

void Whiteboard::resizeEvent(QResizeEvent *e)
{
	QSize newSize = e->size();
	QPixmap *qt_tempPixmap = new QPixmap(newSize.width(),newSize.height());
	qt_tempPixmap->fill(black);
	bitBlt(qt_tempPixmap,0,0,qt_pixmap,0,0);
	delete qt_pixmap;
	qt_pixmap = qt_tempPixmap;
}


void Whiteboard::mouseMoveEvent(QMouseEvent *e)
{
	qt_currMousePos = e->pos();
	paint->drawLine(qt_prevMousePos,qt_currMousePos);
	emit LocalLineDrawn(qt_prevMousePos,qt_currMousePos);
	qt_prevMousePos = qt_currMousePos;
}

void Whiteboard::mousePressEvent(QMouseEvent *e)
{
	qt_prevMousePos = e->pos();
	paint->drawLine(qt_prevMousePos,qt_prevMousePos);
	emit LocalLineDrawn(qt_prevMousePos,qt_prevMousePos);
}

void Whiteboard::mouseReleaseEvent(QMouseEvent *)
{
	bitBlt(qt_pixmap,0,0,this,0,0);
}