2016-09-29 24 views
1

我曾想過關於我編寫的部分代碼的一些意見。我的UI由一個QTableWidget組成,其中有2列,其中2列中的一列填充了QComboBox。如果列中的1沒有任何值,則隱藏QTableWidget中的行

對於第一列,它將使用它在場景中找到的字符裝備列表(完整路徑)來填充單元格,而第二列將爲每個單元格創建一個組合框,並將它填充到顏色選項中作爲選項來自json文件。

現在我試圖創建一些單選按鈕,讓用戶可以選擇顯示所有結果,或者如果該特定行的組合框內沒有顏色選項,它將隱藏這些行。

正如你可以在我的代碼中看到的,我填充每列的數據,所以,當我試圖把它放在if not len(new_sub_name) == 0:,雖然它沒有放置任何零選項的組合框,但我怎麼去隱藏這樣的那些在組合框中沒有選項的行?

def populate_table_data(self): 
    self.sub_names, self.fullpaths = get_chars_info() 

    # Output Results 
    # self.sub_names : ['/character/nicholas/generic', '/character/mary/default'] 
    # self.fullpaths : ['|Group|character|nicholas_generic_001', '|Group|character|mary_default_001'] 

    # Insert fullpath into column 1 
    for fullpath_index, fullpath_item in enumerate(self.fullpaths): 
     new_path = QtGui.QTableWidgetItem(fullpath_item) 
     self.character_table.setItem(fullpath_index, 0, new_path) 
     self.character_table.resizeColumnsToContents() 

    # Insert colors using itempath into column 2 
    for sub_index, sub_name in enumerate(self.sub_names): 
     new_sub_name = read_json(sub_name) 

     if not len(new_sub_name) == 0: 
      self.costume_color = QtGui.QComboBox() 
      self.costume_color.addItems(list(sorted(new_sub_name))) 
      self.character_table.setCellWidget(sub_index, 1, self.costume_color) 

回答

2

您可以使用setRowHidden隱藏行。至於其他代碼,我沒有看到你現在有什麼錯誤,但FWIW我會寫這樣的東西(當然,完全未經測試):

def populate_table_data(self): 
    self.sub_names, self.fullpaths = get_chars_info() 
    items = zip(self.sub_names, self.fullpaths) 
    for index, (sub_name, fullpath) in enumerate(items): 
     new_path = QtGui.QTableWidgetItem(fullpath) 
     self.character_table.setItem(index, 0, new_path) 
     new_sub_name = read_json(sub_name) 
     if len(new_sub_name): 
      combo = QtGui.QComboBox() 
      combo.addItems(sorted(new_sub_name)) 
      self.character_table.setCellWidget(index, 1, combo) 
     else: 
      self.character_table.setRowHidden(index, True) 

    self.character_table.resizeColumnsToContents() 
+0

嘿謝謝,但任何見解關於我如何填充我的數據? – dissidia

+0

@dissidia。不知道它對你有多大用處,但我已經擴大了我的答案。 – ekhumoro

+0

我一定會檢查出來,忘記把我的代碼帶回家。但即便如此,我可以問爲什麼'(sub_name,fullpath)'被括在一起?我想這只是優化和縮短我的代碼? – dissidia

相關問題