Important: Please read the Qt Code of Conduct -

Adding Columns to a QSqlProxyModel

  • Hi All,

    I've searched through several articles on how to add extra columns to a QSqlProxyModel, but none of them do a good job explaining how this could be done ( or done cleanly ).

    Here's my question:

    I have a simple QSqlModelProxy that has 4 columns. I want to generate a 5 column on the fly that is function of columns 2 and 3. I do not want this extra column saved in the database, what is the best way to implement this ?

    Here's what I've tried:
    I created an new with the class XProxyModel that inherits from QIdentityProxyModel. This class overloads the columnCount() method and returns the desired number of columns (5 ). I also overloaded the headerData method to catch the new columns and emit the proper Header strings. This all seems to work fine, my one problem is in the data method ( which I also overloaded in XProxyModel ), within this method my new columns
    never show up through the QModelIndex, it stops at the true underlying columns.

    What am I doing wrong ?

    My other alternative is to change from a TableView to a TableWidget and manually populate this table through
    a QSqlTableModel/QSqlQuery, is this the right solution ?

    Thanks in advance.

  • I Believe I figured it out. If I had to overload the index method as well to return a proper QModelIndex , does this make sense ? Once I did this the row and column became visible through the data method.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Yes it does since you have to somehow provide indexes to access your "virtual" column

Log in to reply