There is not enough experience and knowledge to solve the problem of replacing the values "1" and "0" with "Working" and "Not working" in the column "Status" of my model. It seems I should be using a delegate or should I override the QAbstractTableModel class? Unfortunately, based on examples from the documentation, I did not really understand which methods must be overridden. Maybe someone could give a small example? I would be very grateful! In general, I want to understand the principle of such manipulations, and by example, you can better understand everything.
model = QtSql.QSqlTableModel()
model.setTable('worker')
model.select()
QTableView.setModel(self.model)
model.setHeaderData(1, QtCore.Qt.Orientation.Horizontal, 'Last Name')
model.setHeaderData(2, QtCore.Qt.Orientation.Horizontal, 'First Name')
model.setHeaderData(3, QtCore.Qt.Orientation.Horizontal, 'Middle Name')
model.setHeaderData(4, QtCore.Qt.Orientation.Horizontal, 'Status')
Do I need something like this solution?
class TableModel(QAbstractTableModel):
def rowCount(self, parent):
return len(rows)
def columnCount(self, parent):
return len(headers)
def data(self, index, role):
if role != Qt.DisplayRole:
return QVariant()
return rows[index.row()][index.column()]
I would be grateful for any hints!
Created such an implementation of the method, tell me, is it correct? The values are displayed as I need, but the record in the database has ceased to be saved, maybe some parameters are missing? I am using DataMapper.
class MyProxyModel(QtCore.QIdentityProxyModel):
def data(self, index, role):
column = index.column()
item = QtCore.QIdentityProxyModel.data(self, index, role)
if role == Qt.ItemDataRole.DisplayRole and column == 4::
if item in ['true', '1', 'enabled']:
return 'Works'
elif item in ['false', '0', 'desabled']:
return 'Doesn t works'
return QtCore.QIdentityProxyModel.data(self, index, role)