Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a Widget for PyQt5 for Phyton that remove all from layout ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 4.7k Views
  • 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 Offline
    S Offline
    Sergei_Rack
    wrote on last edited by
    #1

    // 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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        Sergei_Rack
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          1
          • SGaistS SGaist

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

            S Offline
            S Offline
            Sergei_Rack
            wrote on last edited by
            #5

            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
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • mrjjM mrjj

                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 Offline
                S Offline
                Sergei_Rack
                wrote on last edited by
                #7

                @mrjj Yes, you got it.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0
                  • mrjjM mrjj

                    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 Offline
                    S Offline
                    Sergei_Rack
                    wrote on last edited by
                    #9

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

                    mrjjM S 2 Replies Last reply
                    0
                    • S Sergei_Rack

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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
                      0
                      • S Sergei_Rack

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

                        S Offline
                        S Offline
                        Sergei_Rack
                        wrote on last edited by
                        #11

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

                        mrjjM 1 Reply Last reply
                        0
                        • S Sergei_Rack

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          Yes later this evening i can.

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @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 Offline
                            S Offline
                            Sergei_Rack
                            wrote on last edited by
                            #13

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

                            mrjjM 1 Reply Last reply
                            0
                            • S Sergei_Rack

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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              S 1 Reply Last reply
                              0
                              • mrjjM mrjj

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

                                S Offline
                                S Offline
                                Sergei_Rack
                                wrote on last edited by
                                #15

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

                                mrjjM 1 Reply Last reply
                                0
                                • S Sergei_Rack

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

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #16

                                  @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
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved