Вместо того, чтобы определять, когда отображается редактор, вы должны использовать сигнал textChanged редактора (QLineEdit):
from PyQt4 import QtCore, QtGui
class StyledItemDelegate(QtGui.QStyledItemDelegate):
textChanged = QtCore.pyqtSignal(str)
editingFinished = QtCore.pyqtSignal()
def createEditor(self, parent, option, index):
editor = super(StyledItemDelegate, self).createEditor(
parent, option, index
)
if isinstance(editor, QtGui.QLineEdit):
editor.textChanged.connect(self.textChanged)
editor.editingFinished.connect(self.editingFinished)
return editor
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.table_widget = QtGui.QTableWidget(4, 4)
self.table_widget.setHorizontalHeaderLabels(("Name", "1", "2", "3"))
delegate = StyledItemDelegate(self.table_widget)
delegate.textChanged.connect(self.filter)
delegate.editingFinished.connect(self.clear_filter)
self.table_widget.setItemDelegateForColumn(0, delegate)
self.list_widget = QtGui.QListWidget()
hlay = QtGui.QHBoxLayout(self)
hlay.addWidget(self.table_widget)
hlay.addWidget(self.list_widget)
for letter1 in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
for letter2 in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
text = letter1 + letter2
it = QtGui.QListWidgetItem(text)
self.list_widget.addItem(it)
@QtCore.pyqtSlot(str)
def filter(self, text):
self.clear_filter()
for r in range(self.list_widget.count()):
it = self.list_widget.item(r)
# filter algorithm
is_showing = text in it.text()
# Hide the row if necessary
it.setHidden(not is_showing)
@QtCore.pyqtSlot()
def clear_filter(self):
for r in range(self.list_widget.count()):
it = self.list_widget.item(r)
it.setHidden(False)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Обновление:
from functools import partial
from PyQt4 import QtCore, QtGui
class StyledItemDelegate(QtGui.QStyledItemDelegate):
textChanged = QtCore.pyqtSignal(int, int, str)
editingFinished = QtCore.pyqtSignal()
def createEditor(self, parent, option, index):
editor = super(StyledItemDelegate, self).createEditor(
parent, option, index
)
if isinstance(editor, QtGui.QLineEdit):
editor.textChanged.connect(
partial(self.textChanged.emit, index.row(), index.column())
)
editor.editingFinished.connect(self.editingFinished)
return editor
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.table_widget = QtGui.QTableWidget(4, 4)
self.table_widget.setHorizontalHeaderLabels(("Name", "1", "2", "3"))
delegate = StyledItemDelegate(self.table_widget)
delegate.textChanged.connect(self.filter)
delegate.editingFinished.connect(self.clear_filter)
self.table_widget.setItemDelegate(delegate)
self.list_widget = QtGui.QListWidget()
hlay = QtGui.QHBoxLayout(self)
hlay.addWidget(self.table_widget)
hlay.addWidget(self.list_widget)
for letter1 in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
for letter2 in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
text = letter1 + letter2
it = QtGui.QListWidgetItem(text)
self.list_widget.addItem(it)
@QtCore.pyqtSlot(int, int, str)
def filter(self, row, column, text):
print(row, column)
self.clear_filter()
for r in range(self.list_widget.count()):
it = self.list_widget.item(r)
# filter algorithm
is_showing = text in it.text()
# Hide the row if necessary
it.setHidden(not is_showing)
@QtCore.pyqtSlot()
def clear_filter(self):
for r in range(self.list_widget.count()):
it = self.list_widget.item(r)
it.setHidden(False)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())