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. QGraphicsItem boundingRect coordinates
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem boundingRect coordinates

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.1k 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
    e_uofu
    wrote on last edited by
    #1

    Hi there,

    I am implementing a QGraphicsItem and I'm having trouble understanding the coordinate systems. Particularly, I do not understand in what coordinates the boundingRect should be.

    MyItem draws a red rectangle across the middle of the screen (see below). I use QPaintDevice's logical coordinates to find the screen width. (This seems weird. Should the item be able to draw in its own coordinate system?). I store the logical coordinate's width in a member variable. This allows me to compute the bounding rect the same way I paint the rectangle. (This also seems weird. The boundingRect method should return a valid result before MyItem::paint gets executed. )

    If the bounding rect were correct I would be able to trigger mouse hover events of MyItem. I cannot do that, however.

    I'd appreciate any help or clarification on this. Thanks.

    The paint method draws a red rectangle.

    @
    void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    // Device defines the coordinate system
    QPaintDevice *device = painter->device();
    m_width = (float) device->width();

    // Draw stuff
    painter->setBrush(Qt::red);
    painter->drawRect(-m_width/2, 0, m_width, 10);
    

    }
    @

    Here is my bounding rect

    @
    QRectF MyItem::boundingRect() const {
    return QRectF(-m_width/2, 0, m_width, 10);
    }
    @

    And here is how I setup the scene inside my mainwindow.

    @
    QGraphicsScene *scene = new QGraphicsScene();
    MyItem *item = new MyItem();
    scene->addItem(item);
    ui->graphicsView->setScene(scene);
    @

    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