- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个非常令人沮丧的问题,试图删除我的应用程序中的 qgraphicsitems。我有一个菜单 Controller ,负责将按钮添加到布局并将它们添加到场景中。这些按钮都与自定义信号和槽相连。当我改变状态时,我想删除这个 Controller 并删除所有这些 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
我从场景中移除每个按钮,断开连接的按钮,然后尝试对它们调用 delete。
我每次都会遇到段错误。场景项目删除正常,断开连接正常工作,但由于某种原因,当我删除项目时,它会抛出段错误并使程序崩溃。
最佳答案
我猜你的 utilities::delete_ptr()
有问题。
但无论如何。如果您要删除发送者或接收者,则无需断开信号。当其中一个被删除时,这会自动完成。
也无需遍历场景中的整个项目列表并删除它们。来电 QGraphicsScene::clear()
会做。即使那样也没有必要,因为您无论如何都要删除场景。
关于c++ - 从 QGraphicsScene 上的 QGraphicsLinearLayout 中删除 QGraphics 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13676402/
我正在努力让 QGraphicsLinearLayout 与 QGraphicsScene 一起工作,以便一组 QGraphicsItem 以线性方式定位在现场。我试过使用以下方法: #include
我遇到了一个非常令人沮丧的问题,试图删除我的应用程序中的 qgraphicsitems。我有一个菜单 Controller ,负责将按钮添加到布局并将它们添加到场景中。这些按钮都与自定义信号和槽相连。
我是一名优秀的程序员,十分优秀!