Is there a Widget for PyQt5 for Phyton that remove all from layout ?

    // Delete all existing widgets, if any, but I need to do this in every layout. I need, if exists a general Widget.

    if ( m_view->layout() != NULL )
    QLayoutItem* item;
    while ( ( item = m_view->layout()->takeAt( 0 ) ) != NULL )
    delete item->widget();
    delete item;
    delete m_view->layout();

  • Lifetime Qt Champion


    Can you explain why you need to empty your layout ?

  • I need to empty the layout because there are confusion in the users system. Also They want to empty the layout because no want to other person know the last search. This interface are used by many people.

  • Lifetime Qt Champion

    Can you explain a bit more what you UI is about ?

  • This images will explain you better. This is the partial solution code implemented. When user select the radio buttons (after image), then empty all the selection when the user push the cancel button (before image). Will nice to have a widget to empty the layout instead to write the first code in all windows.1_1523369381698_before.png 0_1523369381696_after.png

  • Qt Champions 2017

    When you say reset layouts, do you mean the Widgets to revert to blank input?
    LIke uncheck all checkboxes etc ?
    Clearing the layout would remove all widgets from screen so that seems a bit overkill
    if just to reset the input.

  • @mrjj Yes, you got it.

  • Qt Champions 2017

    ok so its reset button and Cancel do not close anything and hence the "old"
    value remains.
    There is no Widget that can do this for you.
    However, you could make a QGroupBox subclass that would add such feature for any
    sub widgets it holds. Or simply a global function that takes QWidget *parent and resets all widgets it owns. Note that such function must know how to reset the widgets you have used.
    Like setText for LineEdit and setCheck for checkboxes etc.

  • @mrjj Thanks for your answer. Will be nice to have a Widget to do so. Maybe we can ask for it to QT.

  • Qt Champions 2017

    Hi, i think its to broad a topic to get a Qt included Widget as often, people use
    other start values than blank and a function to do it is maybe
    10-20 lines unless it must handle ALL widget there is. Some are hard(er) to reset. Like
    views with models etc. But for LineEdit/textEdit and checkboxes its not super complex.

    You can use FindChildren (i assume python also have that) and use q_object_cast to
    convert to actual type and call the right method for each type to set to blank.

  • @Sergei_Rack Could you do an example for me, please.

  • Qt Champions 2017

    Yes later this evening i can.

  • @mrjj said in Is there a Widget for PyQt5 for Phyton that remove all from layout ?:


    There is no equivalent of qobject_cast in PyQt. If you wish to access an overridden superclass method, you must use super.

  • Qt Champions 2017

    oh, i forgot its python
    Will a c++ sample works ?
    I dont use pyt at all.

  • @mrjj Well yes, I try to convert from C++ to Phyton.

  • Qt Champions 2017

    Hi, ok
    I was thinking something like

    void ResetWidgets( QWidget* parent) {
      QList<QWidget*> children = parent->findChildren<QWidget*>();
      foreach (QWidget* widget, children) {
        QLineEdit* LineEdit = qobject_cast<QLineEdit* >(widget);
        if(LineEdit) { LineEdit ->setText(QString()); continue; }
        QCheckBox* CheckBox = qobject_cast<QCheckBox*>(widget);
        if(CheckBox) { CheckBox->setChecked(false); continue;  }
        QPlainTextEdit* PlainTextEdit = qobject_cast<QPlainTextEdit* >(widget);
        if(PlainTextEdit) { PlainTextEdit->setPlainText(QString()); continue;  }
      // add more types

    you could make it far more generic in c++ with templates but im not sure python has
    something like it - so i didnt.

    I cant help with python code but found this

    for row in range(layout.rowCount()):
        for column in range(layout.columnCount()):
            item = layout.itemAtPosition(row, column)
            if item is not None:
                widget = item.widget()
                if isinstance(widget, QLineEdit):

    So its seems qobject_cast is isinstance, and much of the code should be the same
    except here it takes all widgets from the items in the layout.

    alt text

Log in to reply

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