Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    3
    16
    3257
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sergei_Rack last edited by

      // I FOUND PARTIALLY THIS SOLUTION
      // 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();
      }

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Can you explain why you need to empty your layout ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          Sergei_Rack last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply Reply Quote 1
            • S
              Sergei_Rack @SGaist last edited by

              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

              1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion last edited by

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

                S 1 Reply Last reply Reply Quote 0
                • S
                  Sergei_Rack @mrjj last edited by

                  @mrjj Yes, you got it.

                  1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by

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

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      Sergei_Rack @mrjj last edited by

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

                      mrjj S 2 Replies Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion @Sergei_Rack last edited by

                        @Sergei_Rack
                        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.

                        S 1 Reply Last reply Reply Quote 0
                        • S
                          Sergei_Rack @Sergei_Rack last edited by

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

                          mrjj 1 Reply Last reply Reply Quote 0
                          • mrjj
                            mrjj Lifetime Qt Champion @Sergei_Rack last edited by

                            Hi
                            Yes later this evening i can.

                            1 Reply Last reply Reply Quote 0
                            • S
                              Sergei_Rack @mrjj last edited by

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

                              q_object_cast

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

                              mrjj 1 Reply Last reply Reply Quote 0
                              • mrjj
                                mrjj Lifetime Qt Champion @Sergei_Rack last edited by

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

                                S 1 Reply Last reply Reply Quote 0
                                • S
                                  Sergei_Rack @mrjj last edited by

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

                                  mrjj 1 Reply Last reply Reply Quote 0
                                  • mrjj
                                    mrjj Lifetime Qt Champion @Sergei_Rack last edited by mrjj

                                    @Sergei_Rack
                                    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):
                                                    listWidget.addItem(widget.text())
                                    

                                    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

                                    1 Reply Last reply Reply Quote 1
                                    • First post
                                      Last post