简体   繁体   中英

PyQt QTableWidget signal emitted when selecting no rows

I have a QTableWidget in pyQt with single row selection set. I am connecting itemSelectionChanged to call my row selection function and take action on the selected row. I would also like to detect when the user selects inside the QTableWidget , but selects the empty space (no row is selected), so that I can deselect any selected row. Similar to how "windows explorer" works with file selections.

What signal is triggered when selecting the blank area inside a QTableWidget ? How can this be accomplished?

Check mouse-press events to see if the clicked item is None :

class Table(QtGui.QTableWidget):
    def mousePressEvent(self, event):
        if self.itemAt(event.pos()) is None:
            self.clearSelection()
        QtGui.QTableWidget.mousePressEvent(self, event)

Or if you can't subclass, use an event-filter:

class Window(QtGui.QWidget):
    def __init__(self):
        ...
        self.table.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.MouseButtonPress and
            source is self.table.viewport() and
            self.table.itemAt(event.pos()) is None):
            self.table.clearSelection()
        return QtGui.QWidget.eventFilter(self, source, event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM