[PyQt] Master-detail relationship with plain QTableView tables (no SQL database)

Vadym Honcharuk intereco at gmail.com
Thu Jun 17 18:15:54 BST 2010


thanks a lot, Mark!

I made the following things:

from MainForm

self.connect(self.ui.srcTable.selectionModel(),
QtCore.SIGNAL("currentRowChanged(QModelIndex, QModelIndex)"),
self.frmTableFilter)

further:

 def frmTableFilter(self):
	index = self.ui.srcTable.currentIndex()
	if index.isValid():

	    row = index.row()
	    srcCode = self.srcTableModel.data(self.srcTableModel.index(row,
SRCCODE)).toString()    # take ID as text what required by QRegExp


	    filterFrmTable = QtGui.QSortFilterProxyModel() # forming model
	    filterFrmTable.setSourceModel(self.frmTableModel)
	    self.ui.frmTable.setModel(filterFrmTable)
	
	    filterFrmTable.setDynamicSortFilter(True)
            filterFrmTable.setFilterKeyColumn(0)
            regExp = QtCore.QRegExp(srcCode,
QtCore.Qt.CaseInsensitive) # make filter
            filterFrmTable.setFilterRegExp(regExp) # use filter

Seems work correctly and enought simple and shortly but I not sure if
it's right way...

with best regards,
-vadym


2010/6/17 Mark Summerfield <list at qtrac.plus.com>:
> On 2010-06-14, Vadym Honcharuk wrote:
>> thank you, Mark!
>>
>> one more question about applying filter in the context of
>> QSortFilterProxyModel:
> [snip]
>>
>> Question what method is analog of SQL setFilter() in context of
>> QSortFilterProxyModel?
>
> There are two approaches you can take with QSortFilterProxyModel. The
> easiest is to just create an instance and then use the setFilter*()
> methods. Alternatively, create a QSortFilterProxyModel subclass and
> reimplement filterAcceptsRow() returning True for rows that should be
> accepted and False for those that should be dropped.
>
> [snip]
>
> BTW I devote four chapters of my forthcoming book, "Advanced Qt
> Programming", to Qt's model/view classes---the book is C++-based but
> many of the concepts and idioms can easily be translated into PyQt.
>
>
> --
> Mark Summerfield, Qtrac Ltd, www.qtrac.eu
>    C++, Python, Qt, PyQt - training and consultancy
>        "Advanced Qt Programming" - ISBN 0321635906
>



-- 
Vadym Honcharuk, nic-handle:vh20-uanic
PE Inter-Eco,  http://inter-eco.com.ua
PO Box 3381, Vinnytsya, Ukraine, 21032
phone: +38 0432 699-096, tel/fax 550-578
phone mobile: +38 066 7323736, 7323735


More information about the PyQt mailing list