如何将 mousePressEvent 从 QItemDelegate 传递给 QTableView
How to pass mousePressEvent from QItemDelegate to QTableView
代码创建了一个 QTableView。左列预填充了 QLineEdits 代表。右栏未填充任何代表。
当左列的委托 QLineEdit 被点击时,'clicked' 信号被委托项目阻止并且 tableView "cell" 永远不会被选中。
要选择 tableView 项目,mousePressEvent
应该能够一直通过委托项目到达 tableView。除了第 0 行之外,所有其他索引行都不会被选中。如何让它适用于所有模型索引?
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()
代码创建了一个 QTableView。左列预填充了 QLineEdits 代表。右栏未填充任何代表。
当左列的委托 QLineEdit 被点击时,'clicked' 信号被委托项目阻止并且 tableView "cell" 永远不会被选中。
要选择 tableView 项目,mousePressEvent
应该能够一直通过委托项目到达 tableView。除了第 0 行之外,所有其他索引行都不会被选中。如何让它适用于所有模型索引?
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()