Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Removing Widget from QGRaphicsScene without delting it



  • Hello,

    I have Scene where I want to draw some new items, however a Widget inside the scene should stay there.

    My function is following

    void myDrawing::calc()
    {
        // remove the item from the scene so it does not get deleted
        m_scene->removeItem(m_table_widget->graphicsProxyWidget());
        
        // this is why i call it because i want to update the items
        m_scene->clear();
        addTheNewItemsToScene();
    
        // and now put the table widget back
        QGraphicsProxyWidget* table_proxy = m_scene->addWidget(m_table_widget);
        table_proxy->setPos(10,10);
    
        m_view->update();
    }
    

    (m_table_widget (QTableWidget), m_scene (QGraphicsScene)and m_view (QGraphicsView) are created in constructor)

    However the widget is not shown in the scene when calling the function more than once. (only on the first run).
    When I replace the line by QGraphicsProxyWidget* table_proxy = m_scene->addWidget(new QTableWidegt());
    the TableWIdget is shown.
    However it always only works on the first time. So why is it not shown, when removed and added back again? Or where is the error with the adding/removing?

    EDIT: Also calling ->show() does not have any effect. The widget is also not deleted, since i can add it to some layout or set a new parent and then it appears.


  • Lifetime Qt Champion

    Hi,

    Did you also try to create a new QGraphicsProxyWidgetItem by hand and add that one to your scene ?


  • Lifetime Qt Champion

    Hi,

    Did you also try to create a new QGraphicsProxyWidgetItem by hand and add that one to your scene ?



  • @SGaist Thanks. Now its working with

    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
    proxy->setWidget(groupBox);
    

    and then

       m_scene->removeItem(m_table_proxy);
       m_scene->clear();
       m_scene->addItem(m_table_proxy);
    

    Is this behavior intended or should i submit a bug?


  • Lifetime Qt Champion

    I currently don't know but since the behaviour is different, I would fill a bug. If it's intended, the documentation should reflect that.