我继承QtCore.QAbstractItemModel
,显示树状结构。
在我尝试添加"启用/禁用"每个节点/行的功能。
有人知道如何禁用QTreeView小部件中的某些行吗?
更清楚一点,通过"禁用",我的意思是灰色,仍可右键,可见,不可编辑,如图所示。
答案 0 :(得分:3)
而不是QTreeWidget
我使用了treeView。
我的代码:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class TREEWIDGET (QtGui.QWidget):
def __init__(self):
super(TREEWIDGET, self).__init__()
self.initUI()
def initUI(self):
self.treeView = QtGui.QTreeView (self)
self.treeView.setGeometry(QtCore.QRect(10, 10, 311, 321))
self.treeView.setObjectName('treeWidget')
self.model = QtGui.QStandardItemModel()
self.addItems(self.model)
self.treeView.setModel(self.model)
self.treeView.expandAll ()
self.treeView.setSelectionMode (QtGui.QAbstractItemView.ExtendedSelection)
self.button_disable = QtGui.QPushButton(self)
self.button_disable.setGeometry(QtCore.QRect(20, 340, 121, 23))
self.button_disable.setObjectName('pushButton')
self.button_disable.setText ('Disable')
self.button_enable = QtGui.QPushButton(self)
self.button_enable.setGeometry(QtCore.QRect(160, 340, 111, 23))
self.button_enable.setObjectName('pushButton')
self.button_enable.setText ('Ensable All')
self.button_disable.clicked.connect (self.disable)
self.button_enable.clicked.connect (self.enable)
self.resize(350, 400)
#self.addItems ()
self.show()
def addItems (self, parentWidget) :
rootA = QtGui.QStandardItem ('Joint1')
parentWidget.appendRow (rootA)
cParent = rootA
for loop in range (2, 6) :
child = QtGui.QStandardItem ('Joint' + str(loop))
cParent.appendRow (child)
cParent = child
rootB = QtGui.QStandardItem ('Joint_1')
parentWidget.appendRow (rootB)
cParent = rootB
for loop in range (2, 6) :
child = QtGui.QStandardItem ('Joint_' + str(loop))
cParent.appendRow (child)
cParent = child
def disable (self) :
selectedItem = self.treeView.selectedIndexes ()
for eachItem in selectedItem :
print eachItem
currentItem = eachItem.model().itemFromIndex(eachItem)
currentItem.setEnabled(0)
def enable (self) :
self.allItemElements = []
model = self.treeView.model()
widget = model.invisibleRootItem ()
existsItems = self.collectItems (widget)
for eachItem in existsItems :
eachItem.setEnabled(1)
def collectItems (self, widget) :
itemCount = widget.rowCount()
for itLoop in range (0, itemCount, 1) :
self.allItemElements.append (widget.child(itLoop))
self.collectItems (widget.child(itLoop))
return self.allItemElements
def main():
app = QtGui.QApplication(sys.argv)
ex = TREEWIDGET()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 1 :(得分:2)
这将禁用包含所有子项的行(与其他答案不同)。
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class TREEWIDGET (QtGui.QWidget):
def __init__(self):
super(TREEWIDGET, self).__init__()
self.initUI()
def initUI(self):
self.treeWidget = QtGui.QTreeWidget(self)
self.treeWidget.setGeometry(QtCore.QRect(10, 10, 311, 321))
self.treeWidget.setObjectName('treeWidget')
self.treeWidget.setSelectionMode (QtGui.QAbstractItemView.ExtendedSelection)
self.button_disable = QtGui.QPushButton(self)
self.button_disable.setGeometry(QtCore.QRect(20, 340, 121, 23))
self.button_disable.setObjectName('pushButton')
self.button_disable.setText ('Disable')
self.button_enable = QtGui.QPushButton(self)
self.button_enable.setGeometry(QtCore.QRect(160, 340, 111, 23))
self.button_enable.setObjectName('pushButton')
self.button_enable.setText ('Ensable All')
self.button_disable.clicked.connect (self.disable)
self.button_enable.clicked.connect (self.enable)
self.resize(350, 400)
self.addItems ()
self.show()
def addItems (self) :
parentA = QtGui.QTreeWidgetItem (self.treeWidget)
parentA.setText (0, 'Joint1')
parentA.setExpanded (1)
cParentA = parentA
for loop in range (2, 6) :
childA = QtGui.QTreeWidgetItem (cParentA)
childA.setText (0, 'Joint' + str(loop))
childA.setExpanded (1)
cParentA = childA
parentB = QtGui.QTreeWidgetItem (self.treeWidget)
parentB.setText (0, 'Joint_1')
parentB.setExpanded (1)
cParentB = parentB
for loop in range (2, 6) :
childB = QtGui.QTreeWidgetItem (cParentB)
childB.setText (0, 'Joint_' + str(loop))
childB.setExpanded (1)
cParentB = childB
def disable (self) :
selectedItem = self.treeWidget.selectedItems ()
for eachItem in selectedItem :
eachItem.setDisabled (1)
eachItem.setSelected (0)
def enable (self) :
self.allItemElements = []
widget = self.treeWidget.invisibleRootItem()
existsItems = self.collectItems (widget)
if existsItems :
for eachItem in existsItems :
eachItem.setDisabled (0)
def collectItems (self, widget) :
itemCount = widget.childCount ()
for itLoop in range (0, itemCount, 1) :
self.allItemElements.append (widget.child(itLoop))
self.collectItems (widget.child(itLoop))
return self.allItemElements
def main():
app = QtGui.QApplication(sys.argv)
ex = TREEWIDGET()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 2 :(得分:1)
覆盖flags
后代的QAbstractItemModel
方法。像这样:
def flags(self, index):
if not index.isValid():
return 0
# Disable even rows, enable odd rows
if index.row() % 2 == 0:
return QtCore.Qt.NoItemFlags
else:
return QtCore.Qt.ItemIsEnabled
答案 3 :(得分:0)
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class TREEWIDGET (QtGui.QWidget):
def __init__(self):
super(TREEWIDGET, self).__init__()
self.initUI()
def initUI(self):
self.treeWidget = QtGui.QTreeWidget(self)
self.treeWidget.setGeometry(QtCore.QRect(10, 10, 311, 321))
self.treeWidget.setObjectName('treeWidget')
self.treeWidget.setSelectionMode (QtGui.QAbstractItemView.ExtendedSelection)
self.button_disable = QtGui.QPushButton(self)
self.button_disable.setGeometry(QtCore.QRect(20, 340, 121, 23))
self.button_disable.setObjectName('pushButton')
self.button_disable.setText ('Disable')
self.button_enable = QtGui.QPushButton(self)
self.button_enable.setGeometry(QtCore.QRect(160, 340, 111, 23))
self.button_enable.setObjectName('pushButton')
self.button_enable.setText ('Ensable All')
self.button_disable.clicked.connect (self.disable)
self.button_enable.clicked.connect (self.enable)
self.resize(350, 400)
self.addItems ()
self.show()
def addItems (self) :
parentA = QtGui.QTreeWidgetItem (self.treeWidget)
parentA.setText (0, 'Joint1')
parentA.setExpanded (1)
cParentA = parentA
for loop in range (2, 6) :
childA = QtGui.QTreeWidgetItem (cParentA)
childA.setText (0, 'Joint' + str(loop))
childA.setExpanded (1)
cParentA = childA
parentB = QtGui.QTreeWidgetItem (self.treeWidget)
parentB.setText (0, 'Joint_1')
parentB.setExpanded (1)
cParentB = parentB
for loop in range (2, 6) :
childB = QtGui.QTreeWidgetItem (cParentB)
childB.setText (0, 'Joint_' + str(loop))
childB.setExpanded (1)
cParentB = childB
def disable (self) :
selectedItem = self.treeWidget.selectedItems ()
for eachItem in selectedItem :
eachItem.setDisabled (1)
eachItem.setSelected (0)
def enable (self) :
self.allItemElements = []
widget = self.treeWidget.invisibleRootItem()
existsItems = self.collectItems (widget)
if existsItems :
for eachItem in existsItems :
eachItem.setDisabled (0)
def collectItems (self, widget) :
itemCount = widget.childCount ()
for itLoop in range (0, itemCount, 1) :
self.allItemElements.append (widget.child(itLoop))
self.collectItems (widget.child(itLoop))
return self.allItemElements