Results 1 to 10 of 10

Thread: problem with paint and erase in frame

  1. #1
    Join Date
    Apr 2008
    Posts
    8

    Default problem with paint and erase in frame

    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 .

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with paint and erase in frame

    Can you show us the code you use for drawing?

  3. #3
    Join Date
    Apr 2008
    Posts
    8

    Default Re: problem with paint and erase in frame

    I used this code :

    Qt Code:
    1. class TextFrame : public QFrame
    2.  
    3. {
    4.  
    5. protected:
    6.  
    7.  
    8. void paintEvent(QPaintEvent *e);
    9. void mousePressEvent( QMouseEvent *e );
    10. void mouseMoveEvent( QMouseEvent *e );
    11. void resizeEvent ( QResizeEvent * event) ;
    12.  
    13. private:
    14.  
    15. QPainter windowpainter;
    16. QPainter bufferpainter;
    17. QPen pen ;
    18. QPoint _last;
    19. QPixmap _buffer ;
    20. QPixmap _erase ;
    21. bool erase;
    22. int CounterOfPoint ;
    23. Point Points [MaxPoint];
    24.  
    25.  
    26.  
    27. public:
    28. TextFrame(QWidget *parent);
    29. ~TextFrame();
    30. void CheckEraseSize(QMouseEvent *e);
    31. };
    32.  
    33. //********************************
    34. // Definition of Destructor
    35. //********************************
    36.  
    37. TextFrame::~TextFrame()
    38. {
    39. }
    40.  
    41.  
    42. //********************************
    43. // Definition of Constructor
    44. //********************************
    45.  
    46. TextFrame::TextFrame(QWidget *parent) : QFrame(parent)
    47. {
    48. QPixmap a ( QPixmap("T.jpg") ) ;
    49.  
    50. _buffer = a ;
    51.  
    52. CounterOfPoint = 0 ;
    53.  
    54.  
    55. }
    56.  
    57. void TextFrame :: EraseAll()
    58. {
    59.  
    60.  
    61. CounterOfPoint = 0 ;
    62.  
    63. QWidget::erase();
    64.  
    65.  
    66. // this solve problem only when I erase all things in frame, but
    67. // if I erase some thing problem still there ..
    68.  
    69. _buffer = * (QWidget::erasePixmap()) ;
    70.  
    71. QWidget::setErasePixmap( _buffer ) ;
    72.  
    73. }
    74.  
    75.  
    76. void TextFrame :: paintEvent(QPaintEvent*)
    77. {
    78. bitBlt(this , 0 , 0 , &_buffer) ;
    79. }
    80.  
    81.  
    82. //********************************
    83. // Definition of mousePressEvent
    84. //********************************
    85.  
    86. void TextFrame::mousePressEvent( QMouseEvent * e )
    87. {
    88.  
    89. // write on frame :
    90. if ( erase == false )
    91. {
    92. _last = e->pos();
    93.  
    94. // Add point to array of points
    95. Points [CounterOfPoint].x = _last.x() ;
    96. Points [CounterOfPoint].y = _last.y() ;
    97.  
    98. CounterOfPoint++;
    99.  
    100. pen.setWidth ( _currentSize) ;
    101. pen.setColor (_currentColor);
    102.  
    103. windowpainter.begin (this) ;
    104. bufferpainter.begin(& _buffer) ;
    105.  
    106. windowpainter.setPen (pen);
    107. bufferpainter.setPen (pen);
    108.  
    109. windowpainter.drawPoint(_last);
    110.  
    111. windowpainter.end() ;
    112. bufferpainter.end () ;
    113. }
    114.  
    115. // erase from frame :
    116. else
    117. CheckEraseSize(e);
    118.  
    119. } //end if
    120.  
    121.  
    122. //********************************
    123. // Definition of mouseMoveEvent
    124. //********************************
    125.  
    126. void TextFrame::mouseMoveEvent( QMouseEvent *e )
    127. {
    128.  
    129. // write on frame :
    130. if ( erase == false )
    131. {
    132.  
    133. // set size and color
    134. pen.setWidth ( _currentSize) ;
    135. pen.setColor (_currentColor);
    136.  
    137. // begin writing in buffer and in frame
    138. windowpainter.begin (this) ;
    139. bufferpainter.begin(& _buffer) ;
    140.  
    141. // pass pen to painter
    142. windowpainter.setPen (pen);
    143. bufferpainter.setPen (pen);
    144.  
    145. //draw line
    146. windowpainter.drawLine(_last,e->pos());
    147.  
    148. bufferpainter.drawLine(_last,e->pos()) ;
    149.  
    150. //end buffer and painter
    151. windowpainter.end() ;
    152. bufferpainter.end () ;
    153.  
    154. // refresh the last point
    155. _last= e->pos();
    156.  
    157. // Add point to array of points
    158. Points [CounterOfPoint].x = _last.x() ;
    159.  
    160. Points [CounterOfPoint].y = _last.y() ;
    161.  
    162. CounterOfPoint++;
    163.  
    164. }
    165.  
    166. // erase from frame :
    167. else
    168. CheckEraseSize(e);
    169.  
    170. }
    171.  
    172.  
    173.  
    174. void TextFrame :: resizeEvent ( QResizeEvent * event)
    175. {
    176.  
    177. QPixmap save ( _buffer ) ;
    178.  
    179. _buffer.resize ( event -> size () ) ;
    180.  
    181. bitBlt ( &_buffer , 0, 0 , &save ) ;
    182.  
    183. }void TextFrame :: CheckEraseSize(QMouseEvent *e)
    184. {
    185.  
    186. int x = e-> x() ;
    187. int y = e-> y() ;
    188.  
    189. const QRegion Re(x-sizeErase,y-sizeErase,sizeErase*2,sizeErase*2,QRegion::Ellipse);
    190. QWidget::erase(Re);
    To copy to clipboard, switch view to plain text mode 

    I hope that my code is clear for you.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with paint and erase in frame

    If you draw anything outside paintEvent() it will be lost when something obscures your widget. Restrict drawing to paintEvent() only.

  5. #5
    Join Date
    Apr 2008
    Posts
    8

    Default Re: problem with paint and erase in frame

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with paint and erase in frame

    Quote Originally Posted by M.A.M View Post
    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.

    Qt Code:
    1. QValueList<QValueList<QPoint> > lines;
    2. QValueList<QPoint> currentLine;
    3. void X:mousePressEvent(QMouseEvent *me){
    4. currentLine.push_back(me->pos());
    5. }
    6.  
    7. void X:mouseMoveEvent(QMouseEvent *me){
    8. currentLine.push_back(me->pos());
    9. update();
    10. }
    11.  
    12. void X:mouseReleaseEvent(QMouseEvent *me){
    13. currentLine.push_back(me->pos());
    14. lines.push_back(currentLine);
    15. currentLine.clear();
    16. update();
    17. }
    18.  
    19. void X::paintEvent(QPaintEvent *pe){
    20. QPainter painter(this);
    21. painter.setPen(Qt::blue);
    22. for(int i=0;i<lines.size();i++)
    23. for(int pt = 0; pt<lines[i].size()-1;pt++)
    24. painter.drawLine(lines[i][pt], lines[i][pt+1]);
    25. painter.setPen(Qt::red);
    26. for(int pt=0;pt<currentLine.size()-1;pt++)
    27. painter.drawLine(currentLine[pt], currentLine[pt+1]);
    28. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2008
    Posts
    8

    Default Re: problem with paint and erase in frame

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with paint and erase in frame

    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.

  9. #9
    Join Date
    Apr 2008
    Posts
    8

    Default Re: problem with paint and erase in frame

    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:

    Qt Code:
    1. void TextFrame :: EraseAll()
    2. {
    3. CounterOfPoint = 0 ;
    4.  
    5.  
    6. _buffer = *(QWidget::erasePixmap ());
    7.  
    8.  
    9. QWidget::setErasePixmap (_buffer) ;
    10.  
    11.  
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    could any one help me

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with paint and erase in frame

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.