Behavior of paint method in QGraphicsItem



  • hello,

    when i inherit QGraphicsItem class and implement paint method,i am getting a problem,

    the paint method is being called twice.What is the reason??

    ie.

    @void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    @{

    @ printf("i entered once:\n");
    @}

    gives me the following output

    i entered once:
    i entered once:

    What is the reason for this?And how can i avoid this...



  • /this is the code/
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "QGraphicsItem"
    #include "stdio.h"
    #include "QGraphicsScene"
    #include "QGraphicsView"
    class Draw:public QGraphicsItem
    {
    public:
    Draw();
    QRectF boundingRect() const;
    QPainterPath shape() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    };

    Draw::Draw()
    {
    }

    QRectF Draw::boundingRect() const
    {
    }

    QPainterPath Draw::shape() const
    {
    }

    void Draw::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    printf("Entered once:\n");
    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Draw *mouse=new Draw();
    QGraphicsScene scene;
    scene.setSceneRect(0, 0, 500, 500);
    scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    mouse->setPos(10,10);
    scene.addItem(mouse);

    QGraphicsView view(&scene);
    view.setRenderHint(QPainter::Antialiasing);
    view.setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
    view.setCacheMode(QGraphicsView::CacheBackground);
    view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view.setDragMode(QGraphicsView::ScrollHandDrag);
    view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
    view.show();
    return a.exec();
    }@

    Output
    Entered once
    Entered once

    [EDIT: code formatting cleanup, Volker]



  • i don't think this is a problem. why do you bother this?



  • Can you modify your previous posts and fix the code snippets? Just put a '@' before the whole snippet, and another one after; not one on each line!

    BTW, why do you care about this?



  • paint method will be called as many time as there is any need of referesh in your view, first its called when you are creating the object second time it will be called when you show it on your view. and it will be called many other time if you minized or maximized etc




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.