从 QGraphicsScene 上的 QGraphicsLinearLayout 中删除 QGraphics 项目
Delete QGraphics Items from QGraphicsLinearLayout on QGraphicsScene
我遇到了一个非常令人沮丧的问题,试图删除应用程序中的qgraphicsitems。我有一个菜单控制器,负责将按钮添加到布局并将它们添加到场景中。这些按钮都与自定义信号和插槽连接。当我更改状态时,我想删除此控制器并删除所有这些 qgraphicsitems。
以下是我在menu_controller.cpp中添加它们的方式:
QGraphicsWidget * temp;//this is used during iteration to add to the layout
this->layout = new QGraphicsLinearLayout(Qt::Vertical);//q graphics view layout
this->menu = new QGraphicsWidget;//holds the layout
// initialize the proper buttons
(this->game_state->is_logged_in()) ? (this->logged_in()) : (this->not_logged_in());//test whether or not the user is logged in to generate the correct menu
// now iterate through each button and add to the layout
for (int i = 0, z = this->buttons.size(); i < z; i++) {
temp = this->scene->addWidget(this->buttons[i]);//add widget to the scene
this->layout->addItem(temp);//add this widget to the layou
connect(this->buttons[i], SIGNAL(menu_selection(QString)), this, SLOT(set_menu_option(QString)));//connect the button to this
}
// set menu layout as the layout and then add the menu to the scene
this->menu->setLayout(this->layout);
this->position();
this->scene->addItem(this->menu);
最后,我的析构函数如下所示:
QGraphicsScene * scene = this->game_state->get_scene();
QList<QGraphicsItem *> list = scene->items();
QList<QGraphicsItem *>::Iterator it = list.begin();
for (; it != list.end(); ++it)
if (*it)
scene->removeItem(*it);
for (int i = 0, z = this->buttons.size(); i < z; i++)
disconnect(this->buttons[i], 0, 0, 0);//button not connected to anything
// for each deletes each place in memory
for_each(this->buttons.begin(), this->buttons.end(), utilities::delete_ptr());
delete this->layout;//delete the layout container
delete this->menu;//delete the menu
我从场景中移除每个按钮,断开连接的按钮,然后尝试对它们调用删除。
我每次都遇到分段错误。场景项目删除正常,断开连接正常工作,但由于某种原因,当我删除项目时,它会抛出分割错误并导致程序崩溃。
我猜你的utilities::delete_ptr()
有问题.
但无论如何。如果要删除发送方或接收方,则无需断开信号。删除其中一个时会自动完成此操作。
也无需浏览场景中的整个项目列表并删除它们。打电话QGraphicsScene::clear()
就可以了。即使这样也没有必要删除场景。
感谢您的帮助。
导致分段错误的原因是小部件与信号连接,因此需要使用 deleteLater() 方法删除。
似乎删除一个元素会发出其他小部件的信号,当发生这种情况时,它找不到内存的位置,因此称为 seg 错误......
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 从 QGraphicsScene 上的 QGraphicsLinearLayout 中删除 QGraphics 项目
- 删除鼠标滚轮QGraphics视图上的滚动功能