使用 Qt Designer 构建时,PyQt QTreeview 不显示 QAbstractItemModel 的第一列

PyQt QTreeview not displaying first column of QAbstractItemModel when built with Qt Designer

我用 QAbstractItemModel 构建了一个 Tree 并用代码测试它:

tree = TreeItem()
app = QtGui.QApplication(sys.argv)
app.setStyle("plastique")
model = TreeModel(tree)
treeView = QtGui.QTreeView()
treeView.show()
treeView.setModel(model)

创建的tree正确(如下图所示)

当我尝试在 QTreeVeiw 的应用程序中使用 Tree 时,使用 Qt Designer 构建它只显示第二列 - Typeinfo(如下图所示).实际上第一列在开始时是看不到的,但是如果我将第 Typeinfo 列的左边缘拉到右边,就会显示第一列,但是没有显示 tree 行。

使用下面的代码,创建了 TreeView

tree_view_widget.py

class TreeViewWidget(QMainWindow):
    def __init__(self, tree=None, parent=None, flags=0):
        super(TreeViewWidget, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        model = TreeModel(tree)
        self.ui.treeView.setModel(model)

tree_view_widget_ui.py

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.setEnabled(True)
        Form.resize(200, 300)
        Form.setMinimumSize(QtCore.QSize(200, 300))
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setMargin(0)
        self.gridLayout.setHorizontalSpacing(0)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.treeView = QtGui.QTreeView(Form)
        self.treeView.setEnabled(True)
        font = QtGui.QFont()
        font.setKerning(False)
        self.treeView.setFont(font)
        self.treeView.setFrameShadow(QtGui.QFrame.Sunken)
        self.treeView.setLineWidth(1)
        self.treeView.setMidLineWidth(0)
        self.treeView.setAutoScrollMargin(16)  # 16
        self.treeView.setTabKeyNavigation(True)
        self.treeView.setDragEnabled(True)
        self.treeView.setDragDropOverwriteMode(True)
        self.treeView.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.treeView.setAutoExpandDelay(-1)
        self.treeView.setIndentation(0)
        self.treeView.setRootIsDecorated(False)
        self.treeView.setUniformRowHeights(True)
        self.treeView.setSortingEnabled(True)
        self.treeView.setAnimated(False)
        self.treeView.setAllColumnsShowFocus(True)
        self.treeView.setHeaderHidden(False)
        self.treeView.setObjectName(_fromUtf8("treeView"))
        self.treeView.header().setDefaultSectionSize(0)
        self.treeView.header().setHighlightSections(False)
        self.treeView.header().setSortIndicatorShown(True)
        self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

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

变化:

self.treeView.setRootIsDecorated(False)

收件人:

 self.treeView.setRootIsDecorated(True)