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. How to implement boundingRect() when zooming?
Forum Updated to NodeBB v4.3 + New Features

How to implement boundingRect() when zooming?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 296 Views
  • 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.
  • D Offline
    D Offline
    dave2
    wrote on last edited by
    #1

    Suppose you have a QGraphicsScene on which you place several QGraphicsItem's.

    You want the user to be able to zoom, so you implement wheelEvent() in your view class (which inherits from QGraphicsView).

    When the user zooms in or out, you want the size of the items to change, except for certain items. Let's say such an item is MyGraphicsSquareItem (which inherits QGraphicsItem): you want the size of MyGraphicsSquareItem's to remain the same, independently of the zoom level.

    In order to keep the size of MyGraphicsSquareItem constant, you do something like this when painting it:

    void MyGraphicsSquareItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        qreal detail = option->levelOfDetailFromTransform(painter->worldTransform());
        [painting code which takes detail into account]
    }
    

    Fine, but how should one implement MyGraphicsSquareItem::boundingRect()? How can MyGraphicsSquareItem::boundingRect() (which takes no parameters) get the current QTransform, and thus compute detail as MyGraphicsSquareItem::paint() does?

    1 Reply Last reply
    1
    • D Offline
      D Offline
      dschiller
      wrote on last edited by
      #2

      That is a very good queston. I am facing exactly the same issue where the painting doesn't transform via painter.resetTransform() but my boundingRect() from the custom QGraphicsItem transforms / changes when changing the scene zoom level.

      src/QALibs/Recorder/GraphicsLine.py

      from PyQt5.QtWidgets import QGraphicsItem
      
      class GraphicsLine(QGraphicsItem):
      ...
          def paint(self, painter: QPainter, option, widget):
              painter.resetTransform()
      
              # Painted items stay untransformed / unchanged no matter which zoom level on the scene
              ...
      
          def boundingRect(self):
              # How to avoid boundingRect() changes on zoom
      
      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