Results 1 to 3 of 3

Thread: QPainter doesn't paint

  1. #1
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QPainter doesn't paint

    Hello!

    I am trying to paint a simple plot (sin) on a screen.
    Maybe my method is a bit wierd I don't know, this is my first paint-expirience.
    Hower, I encountered a problem.

    Here is my code:

    .h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <QPainter>
    7.  
    8.  
    9.  
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13. const double pi = 3.14159;
    14. public:
    15. Widget(QWidget *parent = 0);
    16. ~Widget();
    17.  
    18. private:
    19. QLabel *Hi;
    20. QPainter *paint;
    21. virtual void paintEvent(QPaintEvent * );
    22. };
    23.  
    24. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include "widget.h"
    2. #include <QWidget>
    3. #include <cmath>
    4.  
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. this->setGeometry(100,100,800,500);
    9. paint = new QPainter(this);
    10. }
    11.  
    12. Widget::~Widget()
    13. {
    14.  
    15. }
    16.  
    17. void Widget::paintEvent(QPaintEvent * )
    18. {
    19. paint-> setPen(QPen(Qt::red));
    20.  
    21. for(int i=0;i<1000;i++)
    22. {
    23. qreal x=sin(pi/6+i);
    24. qreal y=sin(pi/6+i);
    25. paint->drawPoint(QPointF(x,y));
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    At first, I just tried to paint in .cpp without void Widget::paintEvent(QPaintEvent * ) like this:
    Qt Code:
    1. #include "widget.h"
    2. #include <QWidget>
    3. #include <cmath>
    4.  
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. this->setGeometry(100,100,800,500);
    9. paint = new QPainter(this);
    10. paint-> setPen(QPen(Qt::red));
    11.  
    12. for(int i=0;i<1000;i++)
    13. {
    14. qreal x=sin(pi/6+i);
    15. qreal y=sin(pi/6+i);
    16. paint->drawPoint(QPointF(x,y));
    17. }
    18. }
    19.  
    20. Widget::~Widget()
    21. {
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    It didn't work. Then I looked throught the Internet and found out that one can paint only in void Widget::paintEvent(QPaintEvent * ), and besides this method must be virtual. I have changed this, it didn't help either.

    then I found that paint(this) should be changed to this paint.begin(viewport());
    done and viewport() "no matching fucntion to call"

    What seems to be the problem here? and could someone help me to fix that?
    Thank you in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter doesn't paint

    Typically you allocate a QPainter on the stack inside paintEvent(). As it goes out of scope QPainter::end() will ensure all painting is completed and resources freed.
    All 1000 of your x,y coordinates has a value between 0 and 1: they all map to a single pixel.
    The paintEvent() function is normally protected.

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

    Blitzor DDD (16th July 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't paint

    Quote Originally Posted by ChrisW67 View Post
    Typically you allocate a QPainter on the stack inside paintEvent().
    That is actually the only supported option for painting onto a widget, due to how the widget buffering is implemented internally.

    This code should even have generated a warning at runtime (creating a QPainter on the widget outside of paintEvent()).

    Creating a painter on viewport() is for painting in a QAbstractScrollArea derived class, in normal widgets the paint device is "this".

    Cheers,
    _

Similar Threads

  1. QPainter Fails to Paint
    By gga96 in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2016, 06:28
  2. Make QPainter paint above all widgets
    By Grzyboo in forum Newbie
    Replies: 5
    Last Post: 3rd November 2015, 20:37
  3. QPainter immediate drawing outside paint event
    By sfcheng77 in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2011, 07:39
  4. How to paint (QPainter) on a label ?
    By d@nyal in forum Newbie
    Replies: 1
    Last Post: 29th May 2010, 19:39
  5. Replies: 3
    Last Post: 30th April 2006, 20:22

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.