Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Behavior of paint method in QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

Behavior of paint method in QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    endla.ravi
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • E Offline
      E Offline
      endla.ravi
      wrote on last edited by
      #2

      /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]

      1 Reply Last reply
      0
      • T Offline
        T Offline
        t3chNo
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          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?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Immii
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              milot.shala
              wrote on last edited by
              #6

              "This article":http://thesmithfam.org/blog/2007/02/03/qt-improving-qgraphicsview-performance/ might help.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved