Replace Widget in Layout



  • I have added a couple of widgets to a QFormLayout.
    Now I want to replace two widgets with two other ones (I want to replace a row).
    My application looks like this:
    !http://dl.dropbox.com/u/2346027/OldLabel.PNG(Old Label)!
    When the "change row" button is clicked, the second label should change to "New Label" and the LineEdit should change to a Combobox.

    This is how I'm trying to do it:
    @
    if(m_bToggleRow)
    {
    m_poLayout->removeWidget(m_poOldLabel);
    m_poLayout->removeWidget(m_poOldLine);
    m_poLayout->insertRow(2, m_poNewLabel, m_poNewCombo);
    m_bToggleRow = false;
    }
    else
    {
    m_poLayout->removeWidget(m_poNewLabel);
    m_poLayout->removeWidget(m_poNewCombo);
    m_poLayout->insertRow(2, m_poOldLabel, m_poOldLine);
    m_bToggleRow = true;
    }
    @

    And this is what it looks like:
    !http://dl.dropbox.com/u/2346027/NewLabel.PNG(New Label)!

    I also tried calling m_poLayout->update(), but it still looks the same.

    Is there a method to replace widgets with other ones? Or how can I do that?

    Thank you for your help!



  • bq. void QLayout::removeWidget ( QWidget * widget )
    Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.
    Note: The ownership of widget remains the same as when it was added.

    The m_poOldLabel and m_poOldLine stay where m_poLayout put them, removeWidget() just removes them from layout. You should call hide() on old widgets (or delete them if you won't need them after changing the row).



  • Thank you! This is working!



  • I've got another question regarding the replacement of widgets.

    If I tab through the widgets, the one which was replaced is now on the last position of the tab order. Is it somehow possible to set this to the same as the one I have replaced?
    E.g. if I've replaced the widget in the second row, that the new one is still on the second position of the tab order?

    I know about setTabOrder, but I would like to know if there is another way to do it easier.



  • you can try fixing it with QWidget::setTabOrder, but it can be ugly a bit



  • Kxyu, saho wrote about it.

    [quote author="saho" date="1320744050"]I know about setTabOrder, but I would like to know if there is another way to do it easier.[/quote]



  • Missed it)


Log in to reply
 

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