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. Inherit from QGraphicsRectItem has curious behavior!
Forum Updated to NodeBB v4.3 + New Features

Inherit from QGraphicsRectItem has curious behavior!

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k 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.
  • J Offline
    J Offline
    jackmack
    wrote on last edited by
    #1

    Hi all,

    I use QGraphicsScene/Scene for displaying some graphic items. I have written my own items which inherits from QGraphicsRectItem. The items are selectable but the stippled frame is not painted at the edges but rather inside the colored rectangle. That's strange.

    When I add directly a QGraphicsRectItem to the scene everything is ok.

    Have anybody an idea what's going wrong?

    Here some snippets:

    @MyItem : public QGraphicsRectItem
    {
    ...
    }

    MyItem::MyItem(QGraphicsItem* parent)
    : QGraphicsRectItem(parent)
    {
    setRect(0, 0, 20, 20);
    setPos(x, y);
    setPen(QPen());
    setBrush(Qt::red);
    }@

    main:
    @scene->addItem(new MyItem()); // curious behavior
    scene->addRect(QRectF(0, 0, 20, 20), QPen(), QBrush(Qt::red)); // works!
    @

    When I compare the returned boundingRect() of both items the rect is the same.

    Thanks a lot.

    (working with Qt 4.8.5, VS2010, XP)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jackmack
      wrote on last edited by
      #2

      Found the problem:
      I depends of the QGraphicsItem::type().
      I have reimplemented this in my sublass correctly acourding the docs.

      But it seems the type() is also used from scene to render the item. When I comment out type() the item is drawn correct. But I need the type() for qgraphicsitem_cast() calls :-)

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        this is default behavior in Qt:
        @
        void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
        QWidget *widget)
        {
        Q_D(QGraphicsRectItem);
        Q_UNUSED(widget);
        painter->setPen(d->pen);
        painter->setBrush(d->brush);
        painter->drawRect(d->rect);

        if (option->state & QStyle::State_Selected)
            qt_graphicsItem_highlightSelected(this, painter, option);
        

        }
        @

        If you wanna get rid of it you need to reimplement the paint() method and replace the last 2 lines with your custom selection frame painting.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jackmack
          wrote on last edited by
          #4

          Sounds good. I need in future also an other selection frame.

          THX

          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