Results 1 to 4 of 4

Thread: How to manage QPainter?

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to manage QPainter?

    Hello,
    all examples I have found up to now create the painter each time they need it but I would like to be able to design a general drawing class, in which a painter is defined with a numpber of common attributes, and then design subclasses using this commonly defined painter. So I have the general class creator definied as:

    vadVisualization::vadVisualization(QAbstractItemMo del *model, vad *app, QWidget *parent,
    int xsize, int ysize, char *name, int debug)
    : QAbstractItemView(parent)
    {
    this->setModel(model);
    [...]
    this->painter = new QPainter(this->viewport());
    }

    and the specific class paintEvent method defined as:

    void
    vadControlPolygon:aintEvent(QPaintEvent *event)
    {
    QAbstractItemView:aintEvent(event);
    setDirtyRegion(this->viewport()->rect());
    this->painter->begin(this->viewport());
    this->painter->setWindow(0, 0, 100, 100);
    [...]
    this->painter->drawPolygon(this->polygon);
    // les sommets
    for (int i=0 ; i<3 ; i++)
    this->itemDelegate()->paint(this->painter, option, this->model()->index(i, 0));
    this->painter->end();
    }

    but I get the following errors:

    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::begin(): Painter is already active.
    You must end() the painter before a second begin()
    QPainter::setWindow(), painter not active
    QPainter::end: Painter is not active, aborted
    QPainter::begin(): Painter is already active.
    You must end() the painter before a second begin()
    QPainter::setWindow(), painter not active
    QPainter::end: Painter is not active, aborted
    QPainter::begin(): Painter is already active.

    so ok I understand that my painter is already active, which means that I do not need to begin() and end() it, and then I change my code by withdrawing the begin() and end() calls. Then I get the following errors:

    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::setWindow(), painter not active
    QPainter::setWindow(), painter not active
    QPainter::setWindow(), painter not active
    QPainter::setWindow(), painter not active
    QPainter::setWindow(), painter not active
    QPainter::setWindow(), painter not active

    so please coulod anybody explains me how painter are managed by Qt and how to cope with this in my case?

  2. #2
    Join Date
    Mar 2006
    Posts
    48
    Thanks
    5
    Thanked 4 Times in 3 Posts

    Default

    don't use painter->begin() and painter->end()
    u need only
    QPainter painter( viewport() );
    Last edited by evgenM; 26th April 2006 at 12:51.

  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: How to manage QPainter?

    Or try:
    Qt Code:
    1. painter = new QPainter( 0 );
    2. ...
    3. painter->begin( this );
    4. ...
    5. painter->end();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to manage QPainter?

    EvgenM: as I wrote in my first message, I already tested this solution and it does not work!
    The right solution is those suggested by Jacek, that is I have to create the painter without attaching it to any devide, and then attach is with begin() each time I need it.
    Thanks for your help Jecek.

Similar Threads

  1. Replies: 5
    Last Post: 15th January 2009, 09:03
  2. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 09:42
  3. use Qpainter to draw lines + problem in my painter
    By ReSu in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2008, 15:44
  4. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  5. Replies: 7
    Last Post: 20th March 2006, 20:03

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.