0

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)
snkip
  • 1
  • 2
  • You can use [QIdentityProxyModel](https://doc.qt.io/qt-5/qidentityproxymodel.html) and implement `data()` by returning the string you need when the column corresponds. – musicamante Dec 01 '21 at 18:15
  • Thanks a lot for the tip! – snkip Dec 02 '21 at 04:13
  • Can someone give a small example on the proxy model? I don't understand how to override the "data" method to replace "0" and "1" with string values for a specific column. I understand that I need to create my own proxy model and override the "data" method, but I lack the skills, and in the examples from the documentation I did not understand anything. Can someone help with an example? – snkip Dec 02 '21 at 06:19
  • You didn't mention that you also needed the altered data to be submitted, and it's not clear what you mean by "I am using DataMapper". Remember to be more specific in your questions and to add *all* required details. For instance, are those fields in column 4 intended to be editable? – musicamante Dec 03 '21 at 13:53
  • Hey! Thank you for your attention! Dealt with the problem of saving! The reason was using QSqlQyeryModel, I replaced it with QSqlTableModel and now everything is saved! The data is changed through a dialog box (form). I open this form by double clicking on the TableView. The data in this column can change through the checkbox that is on the form, in the table I do not need a checkbox. I do not edit the data directly in the table. To link table fields to dialog form fields, I use DataMapper, as I read in the documentation. – snkip Dec 04 '21 at 14:39
  • Do you mean QDataWidgetMapper? So, anyway, did you solve your problem? – musicamante Dec 04 '21 at 14:40
  • Hi! Yes, QDataWidgetMapper. I solved the problem – snkip Dec 05 '21 at 16:19

0 Answers0