Combo Box delegate disappears with QSortFilterProxyModel applied

Heads up! You've already completed this tutorial.

Amit_Khanna | 2021-04-17 07:04:01 UTC | #1

Hi everyone,

I have a QTableView with QAbstractTableModel which contains a ComboBox delegate created with QItemDelegate. All this is working fine.

Now I want to add filtering/sorting capability on this view so I used QSortFilterProxyModel like this

python

self.table_model = DictionaryTableModel() # created with QAbstractTableModel
self.delegate = ComboDelegate(self)
self.ui.table_view.setItemDelegateForColumn(2, self.delegate)

self.proxy_model = QtCore.QSortFilterProxyModel(self)
self.proxy_model.setSourceModel(self.table_model)

# set the model
self.ui.table_view.setModel(self.proxy_model)

Now here sorting works but I lose my delegate from the column and get a NoneType if I try to query this delegate which means that delegate never made it to the proxy model and I have no idea how to fix this.

Please help if anyone has a clue on how to sort this issue.

Thanks, ak

The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]


Salem_Bream | 2021-04-18 03:30:27 UTC | #2

the issue should be in the logic of some of your business codes, the code you posted, should in general work. try to post minimal code that reproduces this issue. regards,


Amit_Khanna | 2021-04-18 06:24:13 UTC | #3

Hi Salem,

Thanks for replying. I found the problem with my code!

I was passing self as a parent to delegate class where self is the main_form QDialog but instead, I had to pass the table view as a parent to the delegate class like ->

self.delegate = ComboDelegate(self.table_view)

thus making my view, parent of the delegate and this worked!!

Cheers, Amit


Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Combo Box delegate disappears with QSortFilterProxyModel applied was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.