一个场景中removeItem移除的QWidget不能重新添加到另一个场景
QWidget removed by removeItem in a scene can't be re-added to another scene
我们有一个 QWidget
,由 addWidget
添加到场景中。我们希望在切换到不同场景时保持它显示。我们的做法是通过removeItem
暂时将其从场景中移除,切换到另一个场景后调用addWidget
重新添加。但是,第二个 addWidget
引发错误:
QGraphicsProxyWidget::setWidget: cannot embed widget 0x7835ec8; already embedded
错误可以通过以下代码重现:
QWidget *widget = new QWidget;
scene->addWidget(widget);
scene->removeItem(widget->graphicsProxyWidget());
scene->addWidget(widget);
似乎removeItem
并没有真正删除嵌入状态。有人知道这里发生了什么吗?或者有什么其他好的方法可以满足我们的要求吗?
Qt版本为Qt 5.3.2。
谢谢!
你是对的,显然removeItem
并没有删除嵌入状态。下面的代码检查嵌入状态是否仍然存在,并使用 setWidget
.
手动将其删除
我建议您为此报告一个 Qt bug,因为它确实看起来像一个错误(您可以简单地将它们指向这个 post,我过去也这样做过)。
QGraphicsScene* scene = new QGraphicsScene();
QWidget *widget = new QWidget;
// check widget has no proxy:
assert( widget->graphicsProxyWidget() == NULL );
// add the item:
scene->addWidget(widget);
// check widget has a proxy attached:
assert( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget );
// remove the item:
scene->removeItem(widget->graphicsProxyWidget());
if ( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget )
{
// widget still has a proxy attached, is this a Qt bug?
// manually unset proxy:
widget->graphicsProxyWidget()->setWidget( NULL );
// check widget has no proxy:
assert( widget->graphicsProxyWidget() == NULL );
}
// add the item:
scene->addWidget(widget);
// check widget has a proxy attached:
assert( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget );
我们有一个 QWidget
,由 addWidget
添加到场景中。我们希望在切换到不同场景时保持它显示。我们的做法是通过removeItem
暂时将其从场景中移除,切换到另一个场景后调用addWidget
重新添加。但是,第二个 addWidget
引发错误:
QGraphicsProxyWidget::setWidget: cannot embed widget 0x7835ec8; already embedded
错误可以通过以下代码重现:
QWidget *widget = new QWidget;
scene->addWidget(widget);
scene->removeItem(widget->graphicsProxyWidget());
scene->addWidget(widget);
似乎removeItem
并没有真正删除嵌入状态。有人知道这里发生了什么吗?或者有什么其他好的方法可以满足我们的要求吗?
Qt版本为Qt 5.3.2。
谢谢!
你是对的,显然removeItem
并没有删除嵌入状态。下面的代码检查嵌入状态是否仍然存在,并使用 setWidget
.
我建议您为此报告一个 Qt bug,因为它确实看起来像一个错误(您可以简单地将它们指向这个 post,我过去也这样做过)。
QGraphicsScene* scene = new QGraphicsScene();
QWidget *widget = new QWidget;
// check widget has no proxy:
assert( widget->graphicsProxyWidget() == NULL );
// add the item:
scene->addWidget(widget);
// check widget has a proxy attached:
assert( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget );
// remove the item:
scene->removeItem(widget->graphicsProxyWidget());
if ( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget )
{
// widget still has a proxy attached, is this a Qt bug?
// manually unset proxy:
widget->graphicsProxyWidget()->setWidget( NULL );
// check widget has no proxy:
assert( widget->graphicsProxyWidget() == NULL );
}
// add the item:
scene->addWidget(widget);
// check widget has a proxy attached:
assert( widget->graphicsProxyWidget() && widget->graphicsProxyWidget()->widget() == widget );