I am using QSqlTableModel, (FilterProxyModel) as custom proxy modelwith QtCore.QSortFilterProxyModel, QTableView,Qcombobox.
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.view = QtWidgets.QTableView(self.centralwidget)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
Below code is the custom filter class to get menu of Columns from tableview :
class FilterProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._filter_value = None
@property
def filter_value(self):
return self._filter_value
@filter_value.setter
def filter_value(self, value):
self._filter_value = value
self.invalidateFilter()
def filterAcceptsRow(self, sourceRow, sourceParent):
if self.filter_value is None:
return True
value = (
self.sourceModel()
.index(sourceRow, self.filterKeyColumn(), sourceParent)
.data(self.filterRole())
)
return value == self.filter_value
Below is the database model:
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("model.db")
db.open()
self.model = QSqlTableModel(self)
self.model.setTable("sheet")
self.model.select()
self.view.setModel(self.model)
self.proxy = FilterProxyModel(self)###
self.proxy.setSourceModel(self.model)
self.view.setModel(self.proxy)
Below is the details of my question
column_names = (["All","Name", "Age", "Adress"])
self.comboBox.addItems([x for x in column_names])
@QtCore.pyqtSlot(str)
def msa_lineEdit_textChanged(self, text):
self.proxy = QtCore.QSortFilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.view.setModel(self.proxy)
search = QtCore.QRegExp( text,
QtCore.Qt.CaseInsensitive,
QtCore.QRegExp.RegExp
)
self.proxy.setFilterRegExp(search)
for example : I tryied with above function with QtCore.QRegExp.RegExp its showing the data in first column, selected "all" in Qcombobox.
I added items "All","Name", "Age", and "Adress" in the combobox, and I am trying to search or filter data from QlineEdit with using of QcomboBox as if I select "All" in cobobox and when I am typing words in QlineEdit that should be filter data from all columns.
If I select "Name" or "Age", or "Adress" on QcomboBox that should be filter data as per the column name selection in the Qcombobox. My database have String and as well as Int Values. Are there any examples available for this?