Results 1 to 8 of 8

Thread: Dymamic Painting outside paintEvent on mouse movement in QT4

  1. #1
    Join Date
    Jun 2009
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Dymamic Painting outside paintEvent on mouse movement in QT4

    Hi Every body ,

    I am porting my application fron QT3.3 to QT4.5. In my application all the paintings are done on viewport widget of QScrollView(Q3ScrollView in support class). In this application virtual function paintEvent is not implemented. I am trying to port this code to QT4.5 which require all the paintings done in paintEvent function. For the time being i don't want to change my code much so i am using : Qt::WA_PaintOutsidePaintEvent flag. I running my application on Linux but some how this flag is showing expected effect.

    Following is pseudo code :

    Qt Code:
    1. QucsView::QucsView(QWidget *parent) : Q3ScrollView(parent) // Constructor
    2. {
    3. setVScrollBarMode(Q3ScrollView::AlwaysOn);
    4. setHScrollBarMode(Q3ScrollView::AlwaysOn);
    5. viewport()->setPaletteBackgroundColor(QucsSettings.BGColor);
    6. viewport()->setMouseTracking(true);
    7. viewport()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
    8. }
    9.  
    10. void QucsView::MMoveWire2(QMouseEvent *Event) // Called on movement of mouse
    11. // Basically drawing a line on mouse movement
    12. {
    13. setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
    14. QPainter painter(viewport());
    15. painter.setPen(Qt::DotLine);
    16. painter.setCompositionMode(QPainter::RasterOp_NotSource);
    17. painter.drawLine(----);
    18. }
    To copy to clipboard, switch view to plain text mode 
    When i run this it give warning like
    QPainter::setPen: Painter not active and it don't draw as expected....

    This code is working completely fine with QT3.3 except PaintOutsidePaintEvent flag.

    Any ideas what might be wrong here. I will be thankful for ur help

    thx
    shivam
    Last edited by wysota; 7th June 2009 at 00:59. Reason: missing [code] tags

  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: Dymamic Painting outside paintEvent on mouse movement in QT4

    You shouldn't paint outside paint events in Qt4. You have to call update() and do the painting in the proper place.

    If you really have to paint outside the paint event, set the attribute in the constructor and not in the place where you want to paint.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    Hi,

    Thank you very much for replying back.

    I have set that attribute in constructor as well [viewport()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);] But this is also not working out. I get the same warning : painter not active.

    I have tried one other approach as well..
    On one of the forum link i have read that we can draw QImage or QPixmap on widget in paintEvent function and then rest of the painting can be done on QImage outside paintEvent. I have tried this approach and able to get rid of those warnings like painter not active. But i am facing problem in dynamic painting on mouse movement.

    Following is the modified pseudo code:

    Qt Code:
    1. // Constructor
    2. QucsView::QucsView(QWidget *parent) : Q3ScrollView(parent) // Constructor
    3. {
    4. setVScrollBarMode(Q3ScrollView::AlwaysOn);
    5. setHScrollBarMode(Q3ScrollView::AlwaysOn);
    6. viewport()->setPaletteBackgroundColor(QucsSettings.BGColor);
    7. viewport()->setMouseTracking(true);
    8. }
    9.  
    10. // paint Event Function
    11. void QucsView::paintEvent(QPaintEvent *event) Draw Image on viewport
    12. {
    13. QPainter p (this);
    14. main_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
    15. p.drawImage(0,0,main_image);
    16. }
    17.  
    18. // Painting on mouse movement. Called after mouse is moving without any button pressed.. to draw a line on mouse movement.
    19.  
    20. void QucsView::MMoveWire2(QMouseEvent *Event)
    21. {
    22. QPainter painter(&main_image); // Painting on QImage
    23. painter.setPen(Qt::DotLine);
    24. painter.setCompositionMode(QPainter::RasterOp_NotSource);
    25. painter.drawLine(----);
    26. update();
    27. }
    To copy to clipboard, switch view to plain text mode 

    This code execute without any warning. But i am not able to see drawing of line dynamically on movement of mouse. First I left click on QImage to start drawing from a point and then release left click and simply drag mouse to draw line and finally left click at stopping point. When i left click at stopping point, Line get drawn but I cannot see drawing of line dynamically while movement of mouse.

    Is this problem of transparency of widget or buffering or something else. I will really appreciate your help.

    Sorry for such a long post.

    thx
    shivam
    Last edited by wysota; 7th June 2009 at 08:44. Reason: missing [code] tags

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    this happens because u always destroy the image in the paintEvent(). so the paintevent always draws a blank image..

    try this
    Qt Code:
    1. void QucsView:: paintEvent(QPaintEvent *event) Draw Image on viewport
    2. {
    3. QPainter p (this);
    4.  
    5. //This line gives u a fresh empty image to draw :-(.. delete it :-))
    6. //main_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
    7.  
    8. p.drawImage(0,0,main_image);
    9. }
    10.  
    11. //resizeEvent will invalidate the rect
    12. void QucsView:: resizeEvent(QResizeEvent *event)
    13. {
    14. drawMainImage();
    15. }
    16.  
    17. void QucsView::MMoveWire2(QMouseEvent *Event)
    18. {
    19. drawMainImage();
    20. }
    21.  
    22. void QucsView::drawMainImage()
    23. {
    24. //create a fresh image
    25. main_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
    26. //draw it
    27. QPainter painter(&main_image); // Painting on QImage
    28. painter.setPen(Qt:otLine);
    29. painter.setCompositionMode(QPainter::RasterOp_NotS ource);
    30. painter.drawLine(----);
    31. update();
    32. }
    To copy to clipboard, switch view to plain text mode 

    this should work,...
    I too did the same thing when i was porting my Qt3 app to Qt4.
    but finally we decided to go with the Qt4 way and do everything in paintevent, because we had some other complexities. if your painting code is simple enough and do not do stupid things like mine code than go for it .
    Last edited by nish; 7th June 2009 at 07:12. Reason: missing code tags

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    PS-> add CODE tags to your posts... that will attract more ppl who will read and provide a better solution..

  6. #6
    Join Date
    Jun 2009
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    Hi,
    Thank you very much for replying back. I have tried the above also but some how it is also not working. I am not able to see dynamic drawing. It seems something is related to transparency of viewport of Q3ScrollView.

    Thanks again. I will use tag from next time.

    regards:
    shivam

  7. #7
    Join Date
    Jun 2009
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Post Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    Hi ,

    It seems i got the problem. Actually class Qucsview is inherited from Q3ScrollView. Q3ScrollView has a virtual function called drawContents. In code of QT3.3 there is some painting done in drawContents [ painting of grid structure]. Now when i draw a QImage on this widget then that QImage go below to that grid painting and that is why anything is not visible on mouse movement.

    Following is drawContents code:

    Qt Code:
    1. void QucsView::drawContents(QPainter*p,int,int,int,int)
    2. {
    3. Some code for drawing grid
    4. }
    To copy to clipboard, switch view to plain text mode 

    Now in paintEvent function when i draw QImage by following:
    Qt Code:
    1. void QucsView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter p(this);
    4. this.drawImage(0,0,main_image);
    5. }
    To copy to clipboard, switch view to plain text mode 

    The QImage goes below the grid painting. So any action on QImage on mouse movement is not visible. Any suggestions how to deal with this issue.

    thanks
    shivam
    Last edited by wysota; 8th June 2009 at 21:39. Reason: missing [code] tags

  8. #8
    Join Date
    Jun 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dymamic Painting outside paintEvent on mouse movement in QT4

    Hi Shivam,
    did you got the solution, if yes please share the solution, me too facing the similar issue.

    Regards,
    Dav

Similar Threads

  1. Mouse movement problem in QGraphicsView
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 07:01
  2. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23:18
  3. Game mouse movement
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2006, 23:41
  4. setCanvas blocks mouse movement on QtCanvasView
    By YuriyRusinov in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2006, 07:38

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.