Results 1 to 10 of 10

Thread: QPainter

Hybrid View

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

    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().

  2. #2
    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

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

    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

  4. #4
    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

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

    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.

  6. #6
    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();
    }

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

    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

  8. #8
    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
  •  
Qt is a trademark of The Qt Company.