QToolBox remove widget



  • I'd like to remove a widget (not an item!!!) from my QToolBox.
    How can I solve this?
    I'd like to remove the widget what I added in this way: "layout->addWidget(button);"

    Relevant code snippet:
    @
    QMultiMap<int, int> multiMap;
    ....
    toolBox = new QToolBox(this);
    QVBoxLayout* layout;
    QWidget* widget;
    QPushButton* button;

    QSet<int> keys = QSet<int>::fromList(multiMap.keys());
    foreach( int key, keys )
    {
        layout = new QVBoxLayout();
        widget = new QWidget(this);
    
        QList<int> list = multiMap.values(key);
    
        foreach( int value, list)
        {
            QString s = QString("Camera %1").arg(QString::number(value));
            button = new QPushButton(QIcon(":/images/camera_icon.png"), s, this);
            button->setFlat(true);
            layout->addWidget(button);
        }
    
        layout->addStretch();
        widget->setLayout(layout);
        QString s = QString("Camera Group %1").arg(QString::number(key));
        toolBox->addItem(widget, QIcon(":/images/camera_groups_icon.png"), s);
    }
    

    @



  • What you mean by "remove the widget" ?
    If you mean hiding then showing the widget (remove from the layout) you have two ways :

    Remove the widget from the layout by calling QLayou::removeWidget().

    Calling QWidget::hide() on a widget also effectively removes the widget from the layout until QWidget::show() is called.



  • [quote author="issam" date="1349778696"]What you mean by "remove the widget" ?
    If you mean hiding then showing the widget (remove from the layout) you have two ways :

    Remove the widget from the layout by calling QLayou::removeWidget().

    Calling QWidget::hide() on a widget also effectively removes the widget from the layout until QWidget::show() is called.

    [/quote]

    I'd like to remove the button(s). But when I clicked on a button, I get wrong widget. I cannot cast it to qpushbutton.
    I cannot retrieve the current (selected) button (to remove)...



  • bq. broadpeak : I cannot retrieve the current (selected) button (to remove)…

    In this case I suggest to use QToolButton with QButtonGroup you will find it easiest :

    bq. QButtonGroup class provides a container to organize groups of button widgets.

    QButtonGroup provides three methods (signals) that allow you to determine the clicked button by returning its id !!

    and make this small change in your code please:
    @widget = new QWidget(toolBox);@
    or simply :
    @widget = new QWidget;@
    The widget's parent is not required because you will call QToolBox::addItem() !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.