Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从QGraphicscene上的QGraphicsLinearLayout中删除QGraphics项_C++_Qt_Qgraphicsview_Qgraphicsitem_Disconnect - Fatal编程技术网

C++ 从QGraphicscene上的QGraphicsLinearLayout中删除QGraphics项

C++ 从QGraphicscene上的QGraphicsLinearLayout中删除QGraphics项,c++,qt,qgraphicsview,qgraphicsitem,disconnect,C++,Qt,Qgraphicsview,Qgraphicsitem,Disconnect,我遇到了一个非常令人沮丧的问题,试图在我的应用程序中删除QGraphicsSitems。我有一个菜单控制器,负责将按钮添加到布局中,并将它们添加到场景中。这些按钮都与自定义信号和插槽连接。当我更改状态时,我想删除此控制器并删除所有这些QGraphics站点 以下是我如何将它们添加到我的菜单_controller.cpp中: QGraphicsWidget * temp;//this is used during iteration to add to the layout this-&

我遇到了一个非常令人沮丧的问题,试图在我的应用程序中删除QGraphicsSitems。我有一个菜单控制器,负责将按钮添加到布局中,并将它们添加到场景中。这些按钮都与自定义信号和插槽连接。当我更改状态时,我想删除此控制器并删除所有这些QGraphics站点

以下是我如何将它们添加到我的菜单_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);
QGraphicsWidget*temp//这在迭代期间用于添加到布局
此->布局=新的QGraphicsLinearLayout(Qt::垂直)//q图形视图布局
此->菜单=新的QGraphicsWidget//保持布局
//初始化正确的按钮
(此->游戏状态->是否已登录()?(此->登录()):(此->未登录())//测试用户是否已登录以生成正确的菜单
//现在遍历每个按钮并添加到布局中
对于(inti=0,z=this->buttons.size();iscene->addWidget(this->buttons[i]);//将widget添加到场景中
此->布局->添加项(临时);//将此小部件添加到布局
连接(此->按钮[i],信号(菜单选择(QString)),此,插槽(设置菜单选项(QString));//将按钮连接到此
}
//将“菜单布局”设置为布局,然后将菜单添加到场景中
此->菜单->设置布局(此->布局);
此->位置();
此->场景->添加项(此->菜单);
最后,我的析构函数如下所示:

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
qgraphicscene*scene=this->game_state->get_scene();
QList list=场景->项目();
QList::Iterator it=list.begin();
for(;it!=list.end();++it)
如果(*it)
场景->移除项目(*它);
对于(inti=0,z=this->buttons.size();i按钮[i]、0、0、0)//按钮没有连接到任何东西
//for each删除内存中的每个位置
对于每个按钮(this->buttons.begin()、this->buttons.end()、utilities::delete_ptr());
删除此->布局//删除布局容器
删除此->菜单//删除菜单
我从场景中移除每个按钮,断开连接的按钮,然后尝试调用delete


每次我都会遇到一个分割错误。场景项可以很好地移除,断开连接也可以正常工作,但由于某种原因,当我删除这些项时,它会引发分段错误并导致程序崩溃。

我猜您的
实用程序::delete_ptr()
中有问题

但无论如何。如果要删除发送方或接收方,则无需断开信号。当其中一个被删除时,会自动执行此操作


也不需要遍历场景中的整个项目列表并删除它们。打电话就行了。即使这样,您也不必删除场景。

谢谢您的帮助

导致分段错误的原因是小部件与信号相连,因此需要使用deleteLater()方法删除

似乎删除一个元素会向其他小部件发出信号,当这种情况发生时,它无法找到内存的位置,因此称为seg故障