[英]QGraphicsScene does not have a function to remove QWidget
QGraphicsScene has an addWidget(QWidget *)
function, but no corresponding removeWidget(QWidget *)
. QGraphicsScene具有
addWidget(QWidget *)
函数,但没有相应的addWidget(QWidget *)
removeWidget(QWidget *)
。 It has only removeItem(QGraphicsItem *)
. 它只有
removeItem(QGraphicsItem *)
。
How do I remove a QWidget? 如何删除QWidget?
Here's a basic sample, see if it works for you. 这是一个基本示例,请看它是否适合您。
QGraphicsScene scene;
QPushButton *button = new QPushButton;
//Add item and obtain QGraphicsProxyWidget
QGraphicsProxyWidget *proxy = scene.addWidget(button);
//Remove item
scene.removeItem(proxy);
Just remember to delete any memory you allocate. 只要记住删除您分配的任何内存即可。
Edit: After OP's comments, maybe this is what you want 编辑:OP的评论后,也许这就是您想要的
//add item
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
proxy = scene.addWidget(button);
//remove item
scene.removeItem(button->graphicsProxyWidget());
Again, remember to delete proxy, or use a smart pointer. 同样,请记住删除代理,或使用智能指针。
I have a working solution figured out, but I do have some warnings appearing in the console. 我找到了一个可行的解决方案,但控制台中确实出现了一些警告。 Here's a basic approximation of what I'm doing:
这是我正在做的事情的基本近似:
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;
Here are the warnings I see though: 这是我看到的警告:
QGraphicsProxyWidget::setWidget: cannot embed widget 0x604a2c8 which is not a toplevel widget, and is not a child of an embedded widget QGraphicsProxyWidget::createProxyForChildWidget: top-level widget not in a QGraphicsScene QGraphicsScene::removeItem: cannot remove 0-item QGraphicsProxyWidget :: setWidget:无法嵌入不是顶级小部件,也不是嵌入式小部件的子级的小部件0x604a2c8 QGraphicsProxyWidget :: createProxyForChildWidget:QGraphicsScene中没有的顶级小部件QGraphicsScene :: removeItem:无法删除0-item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.