我已经使用Tree
构建了QAbstractItemModel
并使用以下代码对其进行测试:
tree = TreeItem()
app = QtGui.QApplication(sys.argv)
app.setStyle("plastique")
model = TreeModel(tree)
treeView = QtGui.QTreeView()
treeView.show()
treeView.setModel(model)
创建的tree
是正确的(如下图所示)。
当我尝试使用Tree
中的QTreeVeiw
Qt Designer
时,使用Typeinfo
构建它只显示第二列 - Typeinfo
(如下图所示)。实际上在开头没有看到第一列,但是如果我将列tree
的左边缘向右拉,则会显示第一列,但是没有显示TreeView
行。
使用以下代码,创建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}}
答案 0 :(得分:2)
更改:
self.treeView.setRootIsDecorated(False)
要:
self.treeView.setRootIsDecorated(True)