-
Bug
-
Resolution: Done
-
P2: Important
-
4.5.2
-
None
-
b8eb2784a1c1a9812d09fca7c8722624f12dbd1c
We have faced an issue that if graphics widget is added into scene within polishEvent handler it will never
get polished. Below test application shows this problem.
It should show two red rectanges at the end if both widgets are polished. Here is also a solution proposal into
QGraphicsScene.
void QGraphicsScenePrivate::_q_polishItems() { const QVariant booleanTrueVariant(true); QList<QGraphicsItem*> items = unpolishedItems; unpolishedItems.clear(); int count(items.count()); while (count--) { QGraphicsItem *item = items.at(count); if (!item->d_ptr->explicitlyHidden) { item->itemChange(QGraphicsItem::ItemVisibleChange, booleanTrueVariant); item->itemChange(QGraphicsItem::ItemVisibleHasChanged, booleanTrueVariant); } if (item->isWidget()) { QEvent event(QEvent::Polish); QApplication::sendEvent((QGraphicsWidget *)item, &event); } } }
example:
#include <QApplication> #include <QGraphicsScene> #include <QGraphicsWidget> #include <QGraphicsView> class RectWidget : public QGraphicsWidget { public: RectWidget(Qt::GlobalColor color, QGraphicsItem *parent=0) : QGraphicsWidget(parent), mColor(color) { } void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { painter->setBrush(QBrush(mColor)); painter->drawRect(boundingRect()); } void polishEvent() { QGraphicsWidget::polishEvent(); mColor = Qt::red; update(); } void resizeEvent(QGraphicsSceneResizeEvent *event) { QGraphicsWidget::resizeEvent(event); if (!parentWidget()) { RectWidget *childWidget = new RectWidget(Qt::black, this); childWidget->setGeometry(QRectF(10,10,30,30)); } } private: Qt::GlobalColor mColor; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QGraphicsScene scene; RectWidget *parentWidget = new RectWidget(Qt::white); scene.addItem(parentWidget); QGraphicsView view(&scene); view.resize(200, 200); view.show(); return app.exec(); }