matt_1
matt_1

Reputation: 81

QTableWidget selecting multiple cells with right mouse button

I have a simple QTableWidget set up like so:

self.table= QtGui.QTableWidget()
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.table.viewport().installEventFilter(self)
self.table.verticalHeader().setResizeMode(2)
self.table.horizontalHeader().setResizeMode(2)

The EventFilter reacts to:

def eventFilter(self, source, event):
    if event.type() == QtCore.QEvent.MouseButtonRelease:
        if event.button() == QtCore.Qt.LeftButton:
            --- LMB action ---
        if event.button() == QtCore.Qt.RightButton:
            --- RMB action ---
    return QtGui.QWidget.eventFilter(self, source, event)

The event works just fine for left mouse button click: it detects both a single click, and selecting multiple cells too. I would like to be able to do the same with right mouse button - right now it only recognizes a RMB click, but I can't select multiple cells. Did I mess somethin up or am I missing something?

EDIT 1: I used self.table.itemEntered.connect(handleItemPressed) suggested by MarkyPython and now I have the following:

def eventFilter(self, source, event):

    def handleItemPressed(item):
        self.table.setItemSelected(item, 1)

    if event.type() == QtCore.QEvent.MouseButtonPress:
        if event.button() == QtCore.Qt.RightButton:
            self.table.itemEntered.connect(handleItemPressed)
    if event.type() == QtCore.QEvent.MouseButtonRelease:
        if event.button() == QtCore.Qt.LeftButton:
            --- LMB action ---
        if event.button() == QtCore.Qt.RightButton:
            --- RMB action ---
    return QtGui.QWidget.eventFilter(self, source, event)

Which works in a way. I am now able to select cells with both right and left mouse button, however the right mouse button selection is a little awkward (as you can imagine, it only selects the items/cells that were hovered over, unlike the classic box selection from left mouse button). So my probem is almost solved

Upvotes: 0

Views: 478

Answers (1)

Mark Skelton
Mark Skelton

Reputation: 3891

I have something similar in a program that I am working on right now. You can use itemEntered for when you are holding down the mouse to select multiple cells and you can use itemPressed to select on left mouse click. The comment on line three shows what you can add if you want to select cell by cell rather than a selection. Just ask if it doesn't work.

self.table= QtGui.QTableWidget()
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
# self.table.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
self.table.verticalHeader().setResizeMode(2)
self.table.horizontalHeader().setResizeMode(2)  
self.table.itemEntered.connect(self.handleItemHovered)  # handleItemHoverd is a function you create to do something when you select multiple cells
self.table.itemPressed.connect(self.handleItemPressed)  # handleItemPressed is a function you create to determine what to do when you click on the item

Upvotes: 1

Related Questions