2

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.

Pablo Flores
  • 667
  • 1
  • 13
  • 33
  • Possible duplicate of [How to remove QWidgets from QSplitter](http://stackoverflow.com/questions/371599/how-to-remove-qwidgets-from-qsplitter) – ahmed Jan 30 '16 at 14:21
  • Thank for your advice, but it is not a duplicate because i am asking for delete a "specific" widget. The one that you are showing (i saw this one) deletes all the widget. – Pablo Flores Jan 30 '16 at 14:25
  • First you need to get the widget, by index or search in childs, then hide it. – ahmed Jan 30 '16 at 15:12
  • Are you talking of `QSplitter.indexOf()` . I tried that, but i do not know what to do with that. I am sorry, i´m new in Python – Pablo Flores Jan 30 '16 at 15:23

1 Answers1

3

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.

x squared
  • 3,173
  • 1
  • 26
  • 41