Search code examples
c++qtqgraphicsview

QGraphicsScene does not have a function to remove QWidget


QGraphicsScene has an addWidget(QWidget *) function, but no corresponding removeWidget(QWidget *). It has only removeItem(QGraphicsItem *).

How do I remove a QWidget?


Solution

  • 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

    //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.