PyQt QTreeWidget不显示内容

时间:2014-03-13 17:28:17

标签: python python-2.7 pyqt pyqt4 pyside

我使用Qt设计器设计UI(文件: tree_view_widget_ui.py ),我有一个类(文件: tree_view_widget.py ),用于创建窗口小部件并显示数据,但不显示数据。谁能告诉我我做错了什么。

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 个答案:

答案 0 :(得分:1)

您没有看到任何添加项的原因是因为您已将树窗口小部件的列数设置为零(在GUI模块中)。

要解决此问题,请返回Qt Designer,选择树窗口小部件并向下滚动到属性编辑器的底部。然后选择 columnCount 的值,然后点击右侧的红色小箭头将其重置为默认值。

不要忘记重新生成GUI模块!