Results 1 to 2 of 2

Thread: Problem with QPainter

  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem with QPainter

    Hi everyone,

    I am using Qpainter to paint on a pixmap by using mouse movements

    The following problems were there -

    1. QPainter::begin: Paint device returned engine == 0, type: 2

    2. While drawing the coordinate system gets screwed up and there is a shift while drawing the lines.

    Here is my part of source code..
    Scaled is the pixmap I scaled to (300,300) after loading the image

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *event)
    2. {
    3.  
    4. if (event->button() == Qt::LeftButton) {
    5.  
    6. lastPoint = event->globalPos();
    7. scribbling = true;
    8. }
    9. }
    10.  
    11. void MainWindow::mouseMoveEvent(QMouseEvent *event)
    12. {
    13. if ((event->buttons() & Qt::LeftButton) && scribbling)
    14.  
    15. drawLineTo(event->globalPos());
    16. }
    17. void MainWindow::paintEvent(QPaintEvent *event)
    18. {
    19. QPainter painter(&scaled);
    20. QRect dirtyRect = event->rect();
    21. painter.drawPixmap(dirtyRect, scaled, dirtyRect);
    22. }
    23. void MainWindow::drawLineTo(const QPoint &endPoint)
    24. {
    25. QPainter painter(&scaled);
    26. painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
    27. Qt::RoundJoin));
    28. //painter.begin(&scaled);
    29. painter.drawLine(lastPoint, endPoint);
    30.  
    31.  
    32. int rad = (myPenWidth / 2) + 2;
    33. update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
    34. lastPoint = endPoint;
    35. //painter.end();
    36. }
    37.  
    38. void MainWindow::mouseReleaseEvent(QMouseEvent *event)
    39. {
    40. if (event->button() == Qt::LeftButton && scribbling) {
    41. drawLineTo(event->globalPos());
    42. scribbling = false;
    43. }
    44. ui->Image -> setPixmap (scaled);
    45. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 3rd March 2011 at 14:06. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QPainter

    You are not allowed to draw out side a paint event.
    You have to change your design such, that all drawing happens only in the paintEvent().
    One why would be to store the coordinates during the mouse events and in the mouse event call update() which it turn will call paintEvent(), in which you can then use the saved coordinates to draw.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    bhavya (3rd March 2011)

Similar Threads

  1. Problem with QPainter
    By vijay_kansal in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2010, 13:55
  2. problem in QPainter::begin
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2009, 11:31
  3. QPainter problem (a HW issue perhaps?)
    By jonks in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2009, 04:05
  4. Qt 2.3.10 QPainter problem on arm
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 7th October 2008, 13:29
  5. QT 4.4.0 Upgrade QPainter Problem
    By ChrisReath in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 16:25

Tags for this Thread

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.