2016-01-28 54 views
1

我試圖將列表中的類對象作爲QWidget添加到QSplitter將列表中的類對象添加到QWidget中

這是代碼:

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 

首先,我調用類對象plot和我追加到list_1當我推鍵的組合,然後我需要補充的是特定對象,從列表,在QSplitter使用addWidget

我怎樣才能做到這一點?希望您能夠幫助我。

我需要這樣做,爲了從列表中識別對象,以後我可以創建另一種方法從分隔符中刪除這個對象。

+0

有關使用字典什麼? – tobilocker

+0

謝謝你的回答。有什麼不同? –

+0

您是否嘗試添加剛剛添加到列表中的項目或列表中的其他項目? –

回答

2

由於您已經有了要添加到QSplitter中的對象的引用,因此不需要將其從列表中拉出。

def methodA(self): 
    plot = Plot() 

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

如果你沒有到窗口小部件的引用,但希望這是最近添加到列表中的項目,您可以使用否定列表索引 -

def methodA(self): 
    plot = Plot() 

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