1

I'm trying to inherit QSqlTableModel to make data im my table display in way i need.

class TableViewModel(QSqlTableModel):

    def __init__(self):
        super(TableViewModel, self).__init__()


    def flags(self, modelIndex):
        if not modelIndex.isValid():
            return
        if modelIndex.column() != 1 and modelIndex.column() != 4:
            return Qt.ItemIsEnabled | Qt.ItemIsSelectable
        return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable

    def data(self, modelIndex, role=Qt.DisplayRole):
        if not modelIndex.isValid():
            return QVariant()

        if role != Qt.DisplayRole & role != Qt.EditRole:
            return QVariant()

        return record.value(modelIndex.column())

With this code i'm only getting empty cells. Without data() function this code work perfectly, the data displayed in TableView exactly it should be.

I'm just enmeshed by getting data from QSqlTableModel. Where can i find it? Or is it just my call wrong?

Thanks in advance.

1 Answer 1

2

I'm not sure what that record.value is supposed to be (no indication in your code of where that record variable lives or how or when it's set). Anyway, for "getting data from QSqlTableModel" (whereby I assume you mean the base class you're subclassing), use

whatever = QSqlTableModel.data(self, modelIndex, role)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, it helped. I tried to do it by my own, but mistaked somewhere and as the result - recursion
Thanks are nice, but if this answer helped, what about accepting it? That's SO etiquette (use the checkmark-shaped icon below the number to the left of the answer you want to accept).
maybe super(TableViewModel, self).data(modelIndex, role) is better ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.