如何修改QTreeWidget选择行为

时间:2018-01-16 03:06:32

标签: python pyqt pyqt4 pyqt5 qtreewidget

以下代码会创建一个Line line = new Line(); line.Tag = "Some Value"; line.MouseUp += (sender, args) => { var myValue = ((Line)sender).Tag; Debug.WriteLine($"My value : {myValue}"); }; 。 单击任何项​​目会将其添加到当前选择(QTreeWidget设置为支持QTreeWidget)。但是我想只在使用Shift或Command修饰键时添加到当前选择。

enter image description here

MultiSelection

2 个答案:

答案 0 :(得分:2)

必须覆盖mousePressEventclass TreeWidget(QTreeWidget): def mouseMoveEvent(self, event): if QApplication.keyboardModifiers()&(Qt.ShiftModifier|Qt.ControlModifier): QTreeWidget.mouseMoveEvent(self, event) def mousePressEvent(self, event): if QApplication.keyboardModifiers()&(Qt.ShiftModifier|Qt.ControlModifier): QTreeWidget.mousePressEvent(self, event) if __name__ == '__main__': import sys app = QApplication(sys.argv) widget = TreeWidget() widget.setSelectionMode(QAbstractItemView.MultiSelection) widget.setColumnCount(5) for row in range(7): item = QTreeWidgetItem(widget) for col in range(5): item.setText(col, 'Column %s' % col) widget.show() sys.exit(app.exec_()) 方法,并在其中决定是否采取任何措施。

{"date" : ISODate("2018-01-01T00:00:00Z"), "count" : 3 }
{"date" : ISODate("2018-01-04T00:00:00Z"), "count" : 5 }

答案 1 :(得分:0)

仅通过ExtendedSelection更改“ Multiselection”。将会: widget.setSelectionMode(QAbstractItemView.ExtendedSelection)

文档qt5(版本5.13)https://doc.qt.io/qt-5/qabstractitemview.html#SelectionMode-enum