문제

I used the Qt designer to design UI (file: tree_view_widget_ui.py) and I have a class (file: tree_view_widget.py) that creates the widget and shows the data, but data is not displayed. Can anyone tell me what am I doing wrong.

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

And the GUI design file (created with 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)

Any suggestions how to change the code that the data will be displayed in the tree shape?

도움이 되었습니까?

해결책

The reason why you are not seeing any of your added items, is because you have set the column count of the tree-widget to zero (in the GUI module).

To fix this, go back to Qt Designer, select your tree-widget and scroll down to the bottom of the Property Editor. Then select the value for columnCount and click the little red arrow on the right to reset it back to the default.

And don't forget to re-generate your GUI module!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top