Results 1 to 10 of 10

Thread: QPainter

  1. #1
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter

    Hello,

    I'm trying to display a line with QPainter with following code:
    drawit.h:
    Qt Code:
    1. #ifndef DRAWIT_H
    2. #define DRAWIT_H
    3. #include <QWidget>
    4. class DrawIt : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. DrawIt(QWidget *parent = 0);
    9. protected:
    10. QPainter *paint;
    11. };
    12. #endif
    To copy to clipboard, switch view to plain text mode 

    drawit.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "drawit.h"
    3. DrawIt::DrawIt(QWidget *parent) : QWidget(parent)
    4. {
    5. paint = new QPainter();
    6. }
    7. void DrawIt::paintEvent(QPaintEvent *)
    8. {
    9. paint->drawLine(10, 10, 50, 50);
    10. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include "drawit.h"
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc, argv);
    6. DrawIt *drawIt = new DrawIt;
    7. drawIt->show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    This gives the error "no 'void DrawIt:aintEvent(QPaintEvent *)' member function declared in class DrawIt

    Where am i making the mistake?

    Thx in advance,
    Misko
    Last edited by jacek; 29th July 2007 at 20:08. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    You do not declare the paintEvent function in your class. This function is virtual protected in QWidget. In order to reimplement it in your class you also have to declare it.
    Add the following snippet to your class declaration:
    Qt Code:
    1. protected:
    2. void paintEvent(QPaintEvent* );
    To copy to clipboard, switch view to plain text mode 
    Also, when you draw that line be careful what coordinates you pass because it is possible to draw off-widget. Depends on the dimensions of the widget.

    Also add a forward declare, right before the class declaration, in the header:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    This is needed in order not to include the header in which QPaintEvent is declared. You just tell the compiler that this class is implemented somewhere.

    EDIT: you don't need a member QPainter. Just instantiate one in the paint event, on the stack.
    This is usually done because the painter state may get corrupted( f.e. remain mirrored from a previous pain event ).
    It is better to start with a fresh painter and not to worry about its state.

    Regards
    Last edited by marcel; 29th July 2007 at 20:11.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    Quote Originally Posted by Misko View Post
    DrawIt::DrawIt(QWidget *parent) : QWidget(parent)
    {
    paint = new QPainter();
    }
    Better create that painter in the paintEvent() method and don't forget to pass "this" as a parameter:
    Qt Code:
    1. void DrawIt::paintEvent( QPaintEvent * )
    2. {
    3. QPainter paint( this );
    4. paint.drawLine(10, 10, 50, 50);
    5. }
    To copy to clipboard, switch view to plain text mode 
    As an alternative solution you will have to use QPainter::begin() and QPainter::end().

  4. #4
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter

    @Marcel&Jacek: both thanks!!!
    It works now!
    I find it a lot more difficult to learn C++ because i'm accustomed to Java.
    I find Java more straightforward and thus more easy to learn...
    What about you?

    Regards,
    Misko

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    Well, I do both C++ and Java.
    C++ is much more flexible and fast, when it comes to writing desktop applications.
    On the other hand, while Java may be easier to learn, it is not really the best choice when writing a desktop application(although fully portable). Java is a server side language and it should be treated as such.

    Regards

  6. #6
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter

    Hi again!

    i'm trying the same thing (drawing a line on a widget), but now in the QDevelop-IDE.
    First I create a new Project with a MainWindow.
    Then I see that the MainWindowImpl-constructor has the following:
    class QPaintEvent;
    class QPainter;
    public:
    MainWindowImpl(QWidget *parent = 0, Qt::WFlags f = 0);
    ...
    Then I implement a function in MainWindowImpl.cpp that calls MainWindowImpl:aintEvent...
    So I assume that it should be OK because the MainWindow inherits from QWidget, but on compiling I receive following error in :
    no 'void MainWindowImpl:aintEvent(QPaintEvent *)' member declared in class 'MainWindowImpl'
    Apparently MainWindowImpl doesn't inherit the function paintEvent from QWidget?
    What could be the solution?

    Regards,
    Misko

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    Well, first of all, what you're trying to do is incorrect.
    You must not call paintEvent manually, unless you set a specific flag for the widget, that allows it to be painted outside a paint event( I doubt you need that).

    In qt4 widgets cannot be painted outside paint events.

    To schedule a repaint, you must call update(). This will post a paint event in the widgets event queue, that will be handled when the event loop assumes control.

    So, call update instead.

    However, to solve your problem, how do you call this function?
    Can you post some code?

    Regards.

  8. #8
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter

    Hi Marcel,

    I try to draw a polygon on the main widget.
    Here's the code i've used. It gave me in the debug-version a segmentation fault...
    Poly.h:
    #ifndef POLYIMPL_H
    #define POLYIMPL_H
    #include <QWidget>
    #include "ui_poly.h" /* designer made*/
    class QPaintEvent;
    class QPainter;
    class QPolygon;
    class PolyImpl:ublic QMainWindow, public Ui::Poly
    {
    Q_OBJECT
    public:
    PolyImpl(QWidget *parent = 0, Qt::WFlags f = 0);
    protected:
    void paintEvent(QPaintEvent *);
    private:
    QPainter *painter;
    QPolygon polygoon;
    };
    #endif
    polyimpl.cpp:
    #include <QtGui>
    #include "polyImpl.h"
    PolyImpl::PolyImpl(QWidget *parent, Qt::WFlags f)
    : QMainWindow(parent, f)
    {
    setupUi(this);
    polygoon << QPoint(10, 10) << QPoint(100, 50) << QPoint(75, 75);
    }
    void PolyImpl:aintEvent(QPaintEvent *)
    {
    painter->begin(this);
    painter->drawPolygon(polygoon);
    painter->end();
    }
    main.cpp:
    #include <QApplication>
    #include "polyImpl.h"
    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    PolyImpl win;
    win.show();
    app.connect(&app, SIGNAL(lasWindowClosed()), &app, SLOT(quit()));
    return app.exec();
    }

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    Because you don't instantiate the member QPainter.
    You have two options:
    1. Add:
    Qt Code:
    1. painter = new QPainter();
    To copy to clipboard, switch view to plain text mode 
    in the constructor, and:
    Qt Code:
    1. delete painter;
    To copy to clipboard, switch view to plain text mode 
    in the destructor.

    2. Remove the member( recommended ) and in paint event:
    Qt Code:
    1. QPainter painter(this);
    2. painter.drawPolygon(polygoon);
    To copy to clipboard, switch view to plain text mode 

    Regards

  10. #10
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter

    Thx Marcel!

    It works now!
    Not easy to work with .(dot) and -> operators in C++.
    Better to get a decent C++-manual to read!

    Regards,
    Misko

Similar Threads

  1. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29
  2. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  3. QPainter in console apps?
    By Funklord in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 14:03
  4. Replies: 7
    Last Post: 20th March 2006, 20:03
  5. Replies: 12
    Last Post: 5th February 2006, 10:34

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.