Search code examples
pythonpyqtqwidgetqsplitter

Remove a specific QWidget from a QSplitter


I want to remove a specific Qwidget from a QSplitter (or theQSplitterwith it is the best choice i thing) after i create 2-3 of them.

To do this, i thing that i need to get the index of every widget that i create.

This is my code:

class Windows(self):
  list1 = [widget1, widget2, widget3]
  counter = 0
  def __init__(self):
    #A lot of stuff in here

    self.splitter = QSplitter(Qt.Vertical)

    self.spliiter_2 = QSplitter(Qt.Horizontal)
    self.splitter_2.addwidget(self.splitter)

  def addWidget(self):
    if counter == 1:
      self.splitter.addWidget(Windows.list1[0])
      counter += 1

    if counter == 2:
      counter += 1
      self.splitter.addWidget(Windows.list1[1])

    if counter == 3:
      self.splitter.addWidget(Windows.list1[2])
      counter += 1


  def deleteWidget(self):
    ?????????????? 

As you can see above, i create a QSplitter in the class Windows, and then i create another one (self.splitter_2) where i put the first one.

Every time that i press a combination of keys, i call the addWidget method, and it adds a widget (that depends of the variable counter) from the list1.

If i want to delete the 2nd widget(for example), how can i do it, without deleting the first widget?. I thing that i need the index, but i do not know how to get it.

I have created a button that calls the deleteWidgetmethod, by the way.

I read about the hide() form, but i do not know how to specified it. But, i would rather delete it completely.

Hope you can help me.


Solution

  • Your deleteWidget() function could look like this:

    def deleteWidget(self, index):
        delete_me = self.splitter.widget(index)
        delete_me.hide()
        delete_me.deleteLater()
    

    We need hide() because according to the documentation

    When you hide() a child, its space will be distributed among the other children.