温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Why overriding mousePressEvent in PyQt makes clicking signal doesn't trigger?

python - 为什么在PyQt中重写mousePressEvent会使点击信号不触发?

发布于 2020-03-27 11:10:35

我正在用Python 3.5和PyQt5编写软件。当我重写QTableWidget的mousePressEvent时,cellClicked信号不再触发。我需要重写mousePressEvent来获得鼠标单击的位置,并需要cellClicked来为被单击的单元格做另一项工作。为什么会这样呢?问题出在哪里?这是我重写事件的代码:

class QTableWidget(QTableWidget):
    def checking(self):
        if (self.endgingposx - self.startingposx) >= 50:
            self.clear()
    def mousePressEvent(self,event):
        event.accept()
        self.startingposx = event.x()
    def mouseReleaseEvent(self,event):
        event.accept()
        self.endgingposx = event.x()
        self.checking()

这是我的点击信号,它是连接的插槽:

self.main_table.cellClicked.connect(self.populatescnte)

def populatescnte(self,row,column):
    itemnum = self.scentence_listw.count()
    exec("item{} = QListWidgetItem(QIcon('content/img/food.png'),'{}')".format(itemnum+1,self.main_table.cellWidget(row,column).text))
    exec("self.scentence_listw.addItem(item{})".format(itemnum+1))

self.scentence_listw是一个QListWidget。先感谢您。


更新 谢谢大家,这段代码对我有用:

class myTableWidget(QTableWidget):
def checking(self):
    if (self.endgingposx - self.startingposx) >= 50:
    self.clear()
def mousePressEvent(self,event):
    super().mousePressEvent(event)
    self.startingposx = event.x()
def mouseReleaseEvent(self,event):
    super().mouseReleaseEvent(event)
    self.endgingposx = event.x()
    self.checking()

查看更多

查看更多

提问者
Nimda
被浏览
27
Oluwafemi Sule 2017-08-30 15:55

该事件未传播,因为您已在处理程序中接受该事件。

您允许需要调用原始处理程序。

FWIW,您可以连接处理程序itemSelectionChangedselectedItems在表上获取该值,而不用跟踪位置。

import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget, QTableWidget, 
    QTableWidgetItem, QGridLayout
)


class Widget(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()    

    def initUI(self):
        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Simple')

        table = TrackedTableWidget()

        layout = QGridLayout()
        layout.addWidget(table, 0, 0)
        self.setLayout(layout)

        self.show()


class TrackedTableWidget(QTableWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.initUI()    

    def initUI(self):
        self.setRowCount(2)
        self.setColumnCount(2)
        self.setItem(0, 0, QTableWidgetItem('30', 0));
        self.setItem(0, 1, QTableWidgetItem('40', 0));
        self.setItem(1, 0, QTableWidgetItem('50', 0));
        self.setItem(1, 1, QTableWidgetItem('60', 0));

        self.cellClicked.connect(self._handle_cell_clicked)
        self.itemSelectionChanged.connect(self._handle_item_selection)

    def _handle_cell_clicked(self, row, column):
        print('cell_clicked', row, column)
        print('selected cell positions', self.startingposx, self.endingposx)

    def _handle_item_selection(self, *args):
        print(self.selectedItems())
        print('item_selection', args)

    def mousePressEvent(self, event):
        print('mousePressEvent')
        self.startingposx = event.x()
        super().mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        print('mouseReleaseEvent')
        self.endingposx = event.x()
        super().mouseReleaseEvent(event)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    widget = Widget()

    sys.exit(app.exec_())

参考

发布
问题

分享
好友

手机
浏览

扫码手机浏览