双击QTableWidget的右键单击侦听器

时间:2017-12-24 12:13:53

标签: python pyqt pyqt5 double-click qtablewidget

我有一个PyQt5 QTableWidget,我想要检测双击右键事件。

这是我的'design.py'模块:

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(790, 472)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tbwMain = QtWidgets.QTabWidget(self.centralwidget)
        self.tbwMain.setGeometry(QtCore.QRect(0, 0, 801, 451))
        self.tbwMain.setObjectName("tbwMain")
        self.tabBoxes = QtWidgets.QWidget()
        self.tabBoxes.setObjectName("tabBoxes")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.tabBoxes)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 791, 421))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtWidgets.QSpacerItem(220, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.horizontalLayout.addItem(spacerItem)
        self.tblBoxes = QtWidgets.QTableWidget(self.horizontalLayoutWidget)
        self.tblBoxes.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.tblBoxes.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
        self.tblBoxes.setRowCount(1)
        self.tblBoxes.setObjectName("tblBoxes")
        self.tblBoxes.setColumnCount(3)
        item = QtWidgets.QTableWidgetItem()
        self.tblBoxes.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tblBoxes.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tblBoxes.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        self.tblBoxes.setItem(0, 0, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        self.tblBoxes.setItem(0, 1, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        self.tblBoxes.setItem(0, 2, item)
        self.tblBoxes.horizontalHeader().setStretchLastSection(True)
        self.tblBoxes.verticalHeader().setVisible(False)
        self.horizontalLayout.addWidget(self.tblBoxes)
        spacerItem1 = QtWidgets.QSpacerItem(220, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.horizontalLayout.addItem(spacerItem1)
        self.tbwMain.addTab(self.tabBoxes, "")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        self.tbwMain.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):

        # - - - - -
        self.tblBoxes.doubleClicked.connect(self.tblMouseDoubleClick)

    def tblMouseDoubleClick(self):
        pass #Get event somehow?

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

和我的'main.py'模块一样:

from PyQt5 import QtCore, QtGui, QtWidgets
from design import Ui_MainWindow
import shared.components.tables as tbl
from unshared import boxes as bxs
import switch as swb

stop_threads = False

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        load(self)
    def mouseDoubleClickEvent(self, QMouseEvent):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if event.button() == QtCore.Qt.RightButton:
                print('Double right-click!')

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

我尝试在main.py模块中使用mouseDoubleClickEvent来设置事件;但是,点击仅在表格外的主窗口区域中进行注册。

我也考虑过使用connect(如design.py所示),但我不知道如何检查是否在没有鼠标事件的情况下点击了鼠标右键 - 我不确定是否我有可能得到鼠标事件。我只想这样做,以便当用户双击我表格中的单元格(tblBoxes)时,我可以检测到该事件。

这可能与我提出的任何一种方法有关吗?我正在使用Qt Designer,因此解决方案的侵入性越小越好。

1 个答案:

答案 0 :(得分:2)

您可以使用event-filter来观看MouseButtonDblClick事件:

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        ...
        self.tblBoxes.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.MouseButtonDblClick and
            event.buttons() == QtCore.Qt.RightButton and
            source is self.tblBoxes.viewport()):
            item = self.tblBoxes.itemAt(event.pos())
            if item is not None:
                print('dblclick:', item.row(), item.column())
        return super(MainWindow, self).eventFilter(source, event)