PyQt5 QSortFilterProxyModel hiding QWidget
I have a QStandardItemModel that accomodates data. In one of the columns, I would like to add some QWidgets (clickable pictures). After adding QSortFilterProxyModel for soring/filtering purpose, however, the QSortFilterProxyModel hides my desired QWidgets.
I've searched the Internet but could not find how to keep the QWidget and QSortFilterProxyModel simultaneously. It would be much appreciated if someone may guide me on this issue. Thanks.
A minimal example, using QPushButton as my desired QWidget:
Code:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
def __init__(self):
super().__init__()
self.setLayout(layout)
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
if True: # This shows the buttons in a cell
tab.setModel(sti)
else: # This does not show the buttons
proxy.setSourceModel(sti)
tab.setModel(proxy)
sti.appendRow([])
tab.setIndexWidget(sti.index(1, 2), Buttons())
self.setCentralWidget(tab)
window = MainWindow()
window.resize(800, 600)
window.show()
app.exec_()
Re: PyQt5 QSortFilterProxyModel hiding QWidget
Widgets placed in tables are not part of the item model used for the rest of the table's contents - they belong to the table view, not the model. If you can turn your images into QIcon or QPixmap instances, then you can install them in your model for the Qt::DecorationRole and the table will display them automatically, without the use of a separate QWidget. You have to be sure that your proxy model passes the request for this role to the source model.
Re: PyQt5 QSortFilterProxyModel hiding QWidget
Hi, thank you for your reply. Does your suggestion of "DecorationRole" allow for multiple QPixmap instances in one cell?
Re: PyQt5 QSortFilterProxyModel hiding QWidget
Quote:
Does your suggestion of "DecorationRole" allow for multiple QPixmap instances in one cell?
No, and I don't know if you have much control over the placement of the pixmap within the cell. Possibly the AlignmentRole is observed for icons as well as text. You'll either have to make a separate cell for each pixmap or join the pixmaps into a single one for display. Probably easier to make separate cells, because I don't know if there is a way to know where in a cell the mouse was clicked. Without those coordinates, you can't tell which of the joined pixmaps was clicked.