2014-03-13 68 views
0

我使用Qt的設計師來設計UI(文件:tree_view_widget_ui.py),我有一個類(文件:tree_view_widget.py)創建窗口小部件並顯示數據,但數據不顯示。誰能告訴我我做錯了什麼。PyQt的QTreeWidget不顯示內容

tree_view_widget.py

from tree_view_widget_ui import Ui_Form 

class TreeViewWidget(QWidget): 
    def __init__(self, data2show): 
     QtGui.QWidget.__init__(self,parent) 
     self.ui = Ui_Form() 
     self.ui.setupUi(self) 

     self.data2show = data2show 
     self.addItems(self.ui.treeWidget.invisibleRootItem()) #not sure about this line? 

    def addItems(self, parent): 
     column = 0 
     data_group_1 = self.addParent(parent, column, 'Group1', 'data Clients') 

     for data in self.data2show: 
      self.addChild(data_item, column, data.prop1, data.prop2) 

    def addParent(self, parent, column, title, data): 
     item = QtGui.QTreeWidgetItem(parent, [title]) 
     item.setData(column, QtCore.Qt.UserRole, data) 
     item.setChildIndicatorPolicy(QtGui.QTreeWidgetItem.ShowIndicator) 
     item.setExpanded (True) 
     return item 

    def addChild(self, parent, column, title, data): 
     item = QtGui.QTreeWidgetItem(parent, [title]) 
     item.setData(column, QtCore.Qt.UserRole, data) 
     return item 

和GUI設計文件(使用Qt Designer創建):

tree_view_widget_ui.py

from PyQt4 import QtCore, QtGui 
try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 
try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("TreeView")) 
     Form.setEnabled(True) 
     Form.resize(200, 400) 
     Form.setFixedSize(200, 400) 
     self.treeWidget = QtGui.QTreeWidget(Form) 
     self.treeWidget.setEnabled(True) 
     self.treeWidget.setGeometry(QtCore.QRect(0, 0, 200, 400)) 
     self.treeWidget.setFrameShadow(QtGui.QFrame.Sunken) 
     self.treeWidget.setLineWidth(1) 
     self.treeWidget.setMidLineWidth(0) 
     self.treeWidget.setAutoScrollMargin(16) 
     self.treeWidget.setIndentation(1) 
     self.treeWidget.setRootIsDecorated(True) 
     self.treeWidget.setUniformRowHeights(True) 
     self.treeWidget.setAnimated(False) 
     self.treeWidget.setColumnCount(0) 
     self.treeWidget.setObjectName(_fromUtf8("treeWidget")) 
     self.treeWidget.header().setVisible(False) 
     self.treeWidget.setHeaderHidden(True) 
     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Tree View", "Tree View", None)) 
     self.treeWidget.setSortingEnabled(False) 

任何建議如何改變數據將以樹狀顯示的代碼?

回答

1

之所以沒有看到任何添加的項目,是因爲您已將樹部件的列數設置爲零(在GUI模塊中)。

要解決此問題,請返回到Qt Designer,選擇您的樹形控件並向下滾動到屬性編輯器的底部。然後選擇columnCount的值,然後單擊右側的紅色小箭頭將其重置爲默認值。

不要忘了重新生成你的GUI模塊!