Pablo Flores
Pablo Flores

Reputation: 717

Adding a class object from a list to a QWidget

I am trying to add a class object from a list as a QWidget to a QSplitter .

This is the code:

class Windows(QMainWindow):

  list_1 = []

  def __init__(self):
    #Some stuff in here
    self.splitter = QSplitter(Qt.Vertical)

  def methodA(self):
    plot = Plot()

    Windows.list_1.append(plot)
    self.splitter.addwidget(???)    #Here is where i want to put the specific class object 
                                    #from the list

class Plot():
  #this is a Matplotlib figure

First, i call the class object plot and i append it to list_1 when i push a combination of keys, then i need to add that specific object, from the list, in theQSplitter using addWidget.

How can i do this? Hope you can help me.

I need to do this, in order to identify the object from the list, so later i can create another method to delete this object from the splitter.

Upvotes: 1

Views: 331

Answers (1)

Tim Wakeham
Tim Wakeham

Reputation: 1039

Since you already have a reference to the object you want to add to the QSplitter, you don't need to pull it out of the list.

def methodA(self):
    plot = Plot()

    Windows.list_1.append(plot)
    self.splitter.addwidget(plot)    

If you didn't have a reference to the widget, but wanted to item that was most recently added to the list, you can use negative list indexing -

def methodA(self):
    plot = Plot()

    Windows.list_1.append(plot)
    self.splitter.addwidget(Windows.list_1[-1])       

Upvotes: 2

Related Questions