Inherit from QGraphicsRectItem has curious behavior!
-
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)
-
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 :-)
-
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.
-
Sounds good. I need in future also an other selection frame.
THX