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

时间:2015-01-03 09:48:57

标签: python qt pyqt qt-designer

我已经使用Tree构建了QAbstractItemModel并使用以下代码对其进行测试:

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

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

enter image description here

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

enter image description here

使用以下代码,创建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.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))

tree_view_widget_ui.py

{{1}}

1 个答案:

答案 0 :(得分:2)

更改:

self.treeView.setRootIsDecorated(False)

要:

 self.treeView.setRootIsDecorated(True)