[PyQt] PyQt 4.7 / Qt 4.6.1 QSortFilterProxyModel subclass not emitting signals

Giacomo Lacava g.lacava at gmail.com
Fri Mar 19 11:47:08 GMT 2010


It seems like subclasses of QSortFilterProxyModel will not emit
inherited signals. Tested with PyQt 4.7 / Qt 4.6.1 on Windows XP. I
couldn't find any related entry in Qt bugtracker, so I guess it's
PyQt's fault (unless I've done something wrong).

Here's a simple testcase (expects a ui.py file with a MainWindow with
one pushButton and one listView ).

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt, SIGNAL, QVariant
from ui import *

class MyModel(QtCore.QAbstractTableModel):
    def __init__(self):
	    super(QtCore.QAbstractTableModel,self).__init__()

    def rowCount(self,index=QtCore.QModelIndex()):
        return 3

    def columnCount(self,index=QtCore.QModelIndex()):
        return 3

    def data(self,index=QtCore.QModelIndex(),role=Qt.DisplayRole):
        if index.row() < 0 or index.row() > self.rowCount() or \
            index.column() < 0 or index.column() > self.columnCount():
                return QtCore.QVariant()

        if role == Qt.DisplayRole:
            if index.row() == 1:
                return QVariant("Something")
            else:
                return QVariant("Else")

class MyProxyModel(QtGui.QSortFilterProxyModel):
    def __init__(self, sourceModel):
        super(QtGui.QSortFilterProxyModel,self).__init__()
        self.setSourceModel(sourceModel)
        self.setFilterKeyColumn(0)

    @QtCore.pyqtSlot('QString')
    def setFilterFixedString(self, *args):
        QtGui.QSortFilterProxyModel.setFilterFixedString(self,"Something")


class MyMain(Ui_MainWindow):
    def __init__(self):
        super(Ui_MainWindow,self).__init__()
        self.window = QtGui.QMainWindow()
        self.setupUi(self.window)

        self.model = MyModel()
        self.proxyModel = MyProxyModel(self.model)

        self.listView.setModel(self.proxyModel)
        self.listView.setModelColumn(0)

        self.listView.connect(self.pushButton,SIGNAL("clicked()"),self.proxyModel.setFilterFixedString)
        self.proxyModel.layoutChanged.connect(self.trigger)
        self.window.show()

    def trigger(self,*args):
        print "triggered"

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    tm = MyMain()
    sys.exit(app.exec_())

-- 
Giacomo Lacava


More information about the PyQt mailing list