PDA

View Full Version : problem with paint and erase in frame



M.A.M
14th April 2008, 10:03
Hello every one ..

I am a beginner to qt3 and I have a problem :( . I hope to find a soulition here :)

I paint on the frame . but when another window go over my window, all thinges in my

window disappear !!!

I tried to solve this problem, and I find a soulition, but another problem appear!

now my problem is : when I draw in frame, I also draw in a pixmap, then in paint event

I paint this pixmap in frame.

now when another window go over my window, nothing disappear. but problem here

when I earse from frame, and another window go over my window, all thing, also which I earse it beofore ,appear in window!!
I earse form window using : QWidget::erase ..

any one could help me :(

thanks in advance .

wysota
14th April 2008, 12:08
Can you show us the code you use for drawing?

M.A.M
15th April 2008, 01:17
I used this code :


class TextFrame : public QFrame

{

protected:


void paintEvent(QPaintEvent *e);
void mousePressEvent( QMouseEvent *e );
void mouseMoveEvent( QMouseEvent *e );
void resizeEvent ( QResizeEvent * event) ;

private:

QPainter windowpainter;
QPainter bufferpainter;
QPen pen ;
QPoint _last;
QPixmap _buffer ;
QPixmap _erase ;
bool erase;
int CounterOfPoint ;
Point Points [MaxPoint];



public:
TextFrame(QWidget *parent);
~TextFrame();
void CheckEraseSize(QMouseEvent *e);
};

//********************************
// Definition of Destructor
//********************************

TextFrame::~TextFrame()
{
}


//********************************
// Definition of Constructor
//********************************

TextFrame::TextFrame(QWidget *parent) : QFrame(parent)
{
QPixmap a ( QPixmap("T.jpg") ) ;

_buffer = a ;

CounterOfPoint = 0 ;


}

void TextFrame :: EraseAll()
{


CounterOfPoint = 0 ;

QWidget::erase();


// this solve problem only when I erase all things in frame, but
// if I erase some thing problem still there ..

_buffer = * (QWidget::erasePixmap()) ;

QWidget::setErasePixmap( _buffer ) ;

}


void TextFrame :: paintEvent(QPaintEvent*)
{
bitBlt(this , 0 , 0 , &_buffer) ;
}


//********************************
// Definition of mousePressEvent
//********************************

void TextFrame::mousePressEvent( QMouseEvent * e )
{

// write on frame :
if ( erase == false )
{
_last = e->pos();

// Add point to array of points
Points [CounterOfPoint].x = _last.x() ;
Points [CounterOfPoint].y = _last.y() ;

CounterOfPoint++;

pen.setWidth ( _currentSize) ;
pen.setColor (_currentColor);

windowpainter.begin (this) ;
bufferpainter.begin(& _buffer) ;

windowpainter.setPen (pen);
bufferpainter.setPen (pen);

windowpainter.drawPoint(_last);

windowpainter.end() ;
bufferpainter.end () ;
}

// erase from frame :
else
CheckEraseSize(e);

} //end if


//********************************
// Definition of mouseMoveEvent
//********************************

void TextFrame::mouseMoveEvent( QMouseEvent *e )
{

// write on frame :
if ( erase == false )
{

// set size and color
pen.setWidth ( _currentSize) ;
pen.setColor (_currentColor);

// begin writing in buffer and in frame
windowpainter.begin (this) ;
bufferpainter.begin(& _buffer) ;

// pass pen to painter
windowpainter.setPen (pen);
bufferpainter.setPen (pen);

//draw line
windowpainter.drawLine(_last,e->pos());

bufferpainter.drawLine(_last,e->pos()) ;

//end buffer and painter
windowpainter.end() ;
bufferpainter.end () ;

// refresh the last point
_last= e->pos();

// Add point to array of points
Points [CounterOfPoint].x = _last.x() ;

Points [CounterOfPoint].y = _last.y() ;

CounterOfPoint++;

}

// erase from frame :
else
CheckEraseSize(e);

}



void TextFrame :: resizeEvent ( QResizeEvent * event)
{

QPixmap save ( _buffer ) ;

_buffer.resize ( event -> size () ) ;

bitBlt ( &_buffer , 0, 0 , &save ) ;

}void TextFrame :: CheckEraseSize(QMouseEvent *e)
{

int x = e-> x() ;
int y = e-> y() ;

const QRegion Re(x-sizeErase,y-sizeErase,sizeErase*2,sizeErase*2,QRegion::Ellipse );
QWidget::erase(Re);

I hope that my code is clear for you.

wysota
15th April 2008, 08:18
If you draw anything outside paintEvent() it will be lost when something obscures your widget. Restrict drawing to paintEvent() only.

M.A.M
15th April 2008, 11:04
thank you for your help, but I want to paint in press event and move event function because in my application I want to draw with mouse in frame..

I really need your help :(

wysota
15th April 2008, 11:32
thank you for your help, but I want to paint in press event and move event function because in my application I want to draw with mouse in frame..

Store your drawings in mousePress/Move/Release events but do the drawing in paintEvent.


QValueList<QValueList<QPoint> > lines;
QValueList<QPoint> currentLine;
void X:mousePressEvent(QMouseEvent *me){
currentLine.push_back(me->pos());
}

void X:mouseMoveEvent(QMouseEvent *me){
currentLine.push_back(me->pos());
update();
}

void X:mouseReleaseEvent(QMouseEvent *me){
currentLine.push_back(me->pos());
lines.push_back(currentLine);
currentLine.clear();
update();
}

void X::paintEvent(QPaintEvent *pe){
QPainter painter(this);
painter.setPen(Qt::blue);
for(int i=0;i<lines.size();i++)
for(int pt = 0; pt<lines[i].size()-1;pt++)
painter.drawLine(lines[i][pt], lines[i][pt+1]);
painter.setPen(Qt::red);
for(int pt=0;pt<currentLine.size()-1;pt++)
painter.drawLine(currentLine[pt], currentLine[pt+1]);
}

M.A.M
16th April 2008, 11:39
thank you very much for your help .

but the following error appeard when I run my application before I draw any thing and it appeard in many lines :

ASSERT: "i <= nodes" in /usr/include/qt3/qvaluelist.h (373)

I did not understand this error :( please help me :(

wysota
16th April 2008, 14:09
You are trying to access the list with an index larger than the size of the list. Most probably because you are trying to redraw the widget when there is only one point on the list.

M.A.M
1st May 2008, 20:28
Hello everyone ,,

Thanks so much wysota for your replay ,,


If you draw anything outside paintEvent() it will be lost when something obscures your widget. Restrict drawing to paintEvent() only.
<<<this was my first problem ,, but the piece of code in my second replay sloves this problem (lossing data when another window come over the frame for writing )

to clearly describe my problem ,, here is a brief description of it :
I have a large project , but I have a problem in the interface ,, in my project I need a frame for writing on it ,, and and a button for erasing all what was written , and another button that acts as an eraser and of course a button for re-writing on the frame


The problem is once I write on the frame and erase it ( using QWidget::erase())
and another window come over my window ,, all what was erased before is draw on the frame unhappy



for erasing all the frame I could use this peice of code and the prblem disappears , but I don't know how to do if I would only earase some of my drawing not every thing:


void TextFrame :: EraseAll()
{
CounterOfPoint = 0 ;


_buffer = *(QWidget::erasePixmap ());


QWidget::setErasePixmap (_buffer) ;



}

could any one help me :crying:

wysota
4th May 2008, 21:17
Still my answer remains valid - you shouldn't draw outside paintEvent(). If you do that, you're on your own - don't ask Qt for help.