QGraphicsScene不具有删除QWidget的功能
问题描述:
QGraphicsScene具有addWidget(QWidget *)
功能,但没有对应removeWidget(QWidget *)
。它只有removeItem(QGraphicsItem *)
。QGraphicsScene不具有删除QWidget的功能
如何删除QWidget?
答
这里有一个基本的样本,看看它是否适合你。
QGraphicsScene scene;
QPushButton *button = new QPushButton;
//Add item and obtain QGraphicsProxyWidget
QGraphicsProxyWidget *proxy = scene.addWidget(button);
//Remove item
scene.removeItem(proxy);
只记得删除你分配的任何内存。
编辑:OP的意见后,也许这是你想要
//add item
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
proxy = scene.addWidget(button);
//remove item
scene.removeItem(button->graphicsProxyWidget());
什么再次,记得删除代理,或者使用智能指针。
答
我有一个工作解决方案想通了,但我确实有一些警告出现在控制台中。下面是我在做什么基本近似:
QGraphicsScene *scene = new QGraphicsScene();
// Add button
QPushButton button = new QPushButton("Test", this);
scene->addWidget(button);
// Remove button
QGraphicsProxyWidget *proxy;
proxy = proxy->createProxyForChildWidget(button);
scene_->removeItem(proxy);
delete proxy;
proxy = NULL;
delete button;
button = NULL;
这里是警告我看到虽然:
QGraphicsProxyWidget :: setWidget:不能控件嵌入0x604a2c8这是不是一个*小部件,而不是一个嵌入的小部件 的孩子QGraphicsProxyWidget :: createProxyForChildWidget:*窗口部件不能在QGraphicsScene QGraphicsScene ::的removeItem:不能删除0项
谢谢Maxito。我注意到scene.addWidget返回一个代理小部件指针,并且指出你提供的是做这件事的预期方法。这就是我在我的方法中首先使用代理的原因。在我的实际用例中,小部件指针是一个类成员。我不想创建另一个类成员通过代理指向它,因此我使用createProxy函数来避免这种情况。 – BuvinJ
任何想法是什么关于*小部件的警告? – BuvinJ
我真的不明白你在这里做什么_proxy = proxy-> createProxyForChildWidget(button); _ _button_不是_proxy_中包含的小部件的子元素,_proxy_甚至没有初始化,它不会使感觉到我。 – Maxito