Important: Please read the Qt Code of Conduct -

QsqlQueryModel Subclass

  • This question may have been answered somewhere else already, but I just can't seem to find the answer. I'm using the model to display a set of data from a sql database. I'm using the QSqlQueryModel to retrieve the data.

    I need to add a column to the QSqlQueryModel and allow the user to set some data to the column. therefore i need to subclass the QSqlQueryModel and implement the setData function to realize this. Only the data needs to be saved local and not be committed to the database.

    So is it possible to save the data internal in the QSqlQueryModel? Has someone an example?

    Any help is appreciated, thanks.

  • Lifetime Qt Champion


    Take a look at the example in the Model/View documentation, you'll have a good explanation on how to handle custom models.

    Roughly, you have to make your own container for your custom column data and use it when needed. The rest of the time, call the QSqlQueryModel functions.

    Hope it helps

  • hi,

    Someone else suggested that changing setdata was not a good idee. a better solution is subclassing a QAbstractProxyModel.

    So i made a subclass from QAbstractProxyModel which has a List of pairs as extra column.
    only i cant figure out how to implement al the needed functions.
    currently i am having a model that is displaying the data with a extra column, All the fields are gray. the last column is not editable and every field has a checkbox.

    So can someone show me a better implementation?

    I need a model that i can show with QtableView. -> so it needs subbclassed from QAbstractItemModel or one of his child's.

    The data it needs to contain:
    A result of a select Query. not editable
    A extra column. editable for the user.

    After pushing the submit button. The first column and user editable column are used to submit new records in an other table.

    Any help is appreciated, thanks.

  • Lifetime Qt Champion

    Then, how would you update your internal data structure ?

    If you really don't want to subclass QSqlQueryModel, I would go for a "QIdentityProxyModel":

    Override data, column count, and setData. For the first two, it seems you already know what to do. In setData, call the original setData when the column is not your custom columns otherwise, update your data structure.

    I think you will also need to override flags to tell the view that your custom column is editable (take a look at the example in the documentation to make a QSqlQueryModel writable)

Log in to reply