Reputation: 227
I've got a qTreeWidget-based interface where I double click on individual items to toggle them on and off. However, I'd like to be able to bulk-toggle them by selecting multiple objects and double clicking them, but when you double click on any item you immediately lose the multi-selection.
Does anyone know a way around this?
Many thanks for your time,
Nick
Upvotes: 1
Views: 1788
Reputation: 120608
The selection/deselection of items is controlled by the mouse-press event, which obviously happens before the double-click is registered. So you need to "eat" the mouse-press at the appropriate moment.
This example allows double-clicking when the meta-key is pressed:
from PySide import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.tree = QtGui.QTreeWidget(self)
for text in 'One Two Three Four'.split():
parent = QtGui.QTreeWidgetItem(self.tree, [text])
for text in 'Red Blue Green'.split():
child = QtGui.QTreeWidgetItem(parent, [text])
parent.setExpanded(True)
self.tree.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
self.tree.viewport().installEventFilter(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.tree)
def eventFilter(self, source, event):
if (source is self.tree.viewport() and
isinstance(event, QtGui.QMouseEvent) and
event.modifiers() == QtCore.Qt.MetaModifier):
if event.type() == QtCore.QEvent.MouseButtonDblClick:
print('meta-double-click')
return True
if event.type() == QtCore.QEvent.MouseButtonPress:
# kill selection when meta-key is also pressed
return True
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 300, 300, 300)
window.show()
sys.exit(app.exec_())
Upvotes: 1
Reputation: 600
The first step would be to setup an event that fires whenever an item is double clicked, like so:
treeWidget.itemDoubleClicked.connect(onClickItem)
where onClickItem is:
def onClickItem(item):
print('Text of first column in item is ', item.text(0))
Of course you'll want to do something a bit more fancy inside onClickItem().
Upvotes: 2