python - 如何将 mousePressEvent 从 QItemDelegate 传递给 QTableView

标签 python qt pyqt qtableview qitemdelegate

代码创建了一个 QTableView。左列预填充了 QLineEdits 代表。右栏未填充任何代表。

当左列的委托(delegate) QLineEdit 被点击时,'clicked' 信号被委托(delegate)项阻止并且 tableView“cell”永远不会被选中。

要选择 tableView 项目,mousePressEvent 应该能够一直通过委托(delegate)项目到达 tableView。除了第 0 行之外,所有其他索引行都不会被选中。如何使其适用于所有模型索引?

enter image description here

from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication([])

class LineEdit(QTextEdit):
    def __init__(self, parent=None):
        super(LineEdit, self).__init__(parent)
    def mousePressEvent(self, event):
        tableView = self.parent().parent()
        tableView.mousePressEvent(event)

class Delegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        return LineEdit(parent)

def onClick(index):
    print 'tableView.onClick:', index

tableView = QTableView()
tableView.setModel(QStandardItemModel(4, 2))
tableView.clicked.connect(onClick)
tableView.setItemDelegate(Delegate())

for row in range(4):
    tableView.openPersistentEditor(tableView.model().index(row, 0))

tableView.show()
app.exec_()

user1034749 发布的解决方案:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication([])

class LineEdit(QTextEdit):
    def __init__(self, parent=None):
        super(LineEdit, self).__init__(parent)

    def mouseReleaseEvent(self, event):
        super(LineEdit, self).mouseReleaseEvent(event)
        table = self.parent().parent() # added by me here
        tableView.selectRow(0) # to fix the issue with tableView row not getting selected on delegated click.
        event.ignore()

    def mousePressEvent(self, event):
        super(LineEdit, self).mousePressEvent(event)
        event.ignore()

class Delegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        return LineEdit(parent)

def onClick(index):
    print 'tableView.onClick:', index
    selectedIndexes = tableView.selectionModel().selectedRows()

tableView = QTableView()
tableView.setSelectionBehavior(QTableView.SelectRows)
tableView.setModel(QStandardItemModel(4, 2))
tableView.clicked.connect(onClick)
tableView.setItemDelegate(Delegate())

for row in range(4):
    tableView.openPersistentEditor(tableView.model().index(row, 0))

tableView.show()
app.exec_()

最佳答案

要传递事件,您只需忽略它,如下所示:

 def mouseReleaseEvent(self, event):
        print "mouse release"
        super(LineEdit, self).mouseReleaseEvent(event)
        event.ignore()

 def mousePressEvent(self, event):
        print "mouse press"
        super(LineEdit, self).mousePressEvent(event)
        event.ignore()

关于python - 如何将 mousePressEvent 从 QItemDelegate 传递给 QTableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091178/

相关文章:

python - 查找背景的最佳方法-图像处理Python

python - HTTP header 被 `urllib3.exceptions.HeaderParsingError: [MissingHeaderBodySeparatorDefect()], unparsed data` 切成两半

python - 如何在 python 中使用 diffLib 避免输出文件中出现公共(public)行

c++ - 使用 C++ 将 QML 元素动态添加/插入到 QML View 中

c++ - 使用 QGraphicsEffect 删除 QGraphicsItem 会导致段错误

python - 如何检索 QTableView 的选定行?

python - PyQt错误: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

python - 未找到 Amazon EMR Pyspark 模块

qt - PyQt:将 QTabWidget.tabClos​​eRequested 连接到插槽的正确方法

python - PyQt5/pyqt4 是否已经支持具有手写识别功能的 QtVirtualKeyboard?