Changing column order in QSqlTableModel



  • Hi,
    I'm displaying an SQLlite db table using QSqlTableModel. is it possible to change the order of the columns displayed in the model? Currently it is in the order as they appear in the db.
    Thank you.




  • Moderators

    @gabor53
    shouldn't be that hard to do with QAbstractProxyModel and flipping the column index.

    Or if it's just for displaying using QHeaderView::moveSection() (in the view widget) is the most easiest possibility.



  • @raven-worx
    Hi,
    I'm going with the QHeaderview::movesection().
    I got to the first step:

    QHeaderView *headerView = new QHeaderView(Qt::Horizontal, ui->tableView_Fix *parent() = Q_NULLPTR);
    

    but it gives me an error message:
    C:\Programming\Projects\Folkfriends_1_0\fixdb.cpp:30: error: expected primary-expression before '*' token
    QHeaderView *headerView = new QHeaderView(Qt::Horizontal, QWidget *parent = Q_NULLPTR);
    ^
    Please help me to figure what to change. Thank you.



  • This is the error Part..

    ui->tableView_Fix *parent() = Q_NULLPTR
    

    check this,

    QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget * parent = 0)
    

  • Moderators

    @gabor53
    it's not necessary to create a new instance of QHeaderView. QTreeView and QTableView already have one. So get it from there and call move section on it.



  • Here's how I did it.

    // reorderColProxyModel.h
    #pragma once
    
    #include <QObject>
    #include <QIdentityProxyModel>
    
    class ReOrderColProxyModel : public QIdentityProxyModel
    {
    public:
        ReOrderColProxyModel(QObject *parent = Q_NULLPTR);
        ~ReOrderColProxyModel() = default;
    
        QVariant data(const QModelIndex &proxyIndex, int role) const;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    
        int columnCount(const QModelIndex &) const;
    };
    
    // reorderColProxyModel.cpp
    #include "reorderColProxyModel.h"
    #include "constants.h"
    
    namespace
    {
    // list columns in order to be seen
    constexpr REPAIR_STATUS_COLUMN const FORWARD_LOOKUP[]=
    {
        REPAIR_STATUS_COLUMN::RMA_NUMBER,
        REPAIR_STATUS_COLUMN::WORKDAYS,
    
        REPAIR_STATUS_COLUMN::CUSTOMER_NAME,
        REPAIR_STATUS_COLUMN::LOCATION,
        REPAIR_STATUS_COLUMN::MODEL,
    
        REPAIR_STATUS_COLUMN::RMA_ISSUE_DATE,
        REPAIR_STATUS_COLUMN::RCVD_DATE,
        REPAIR_STATUS_COLUMN::DONE_DATE,
        REPAIR_STATUS_COLUMN::SHIP_DATE,
    
        REPAIR_STATUS_COLUMN::CURRENT_STATUS,
        REPAIR_STATUS_COLUMN::SPECIAL_NOTES,
        REPAIR_STATUS_COLUMN::TECH,
        REPAIR_STATUS_COLUMN::QB_NOTES,
        REPAIR_STATUS_COLUMN::RMA_NOTES,
        REPAIR_STATUS_COLUMN::RMA_UNIT_NOTES
    };
    
    constexpr int const REORDER_FORWARD_LOOKUP_SIZE= sizeof(FORWARD_LOOKUP)/sizeof(FORWARD_LOOKUP[0]);
    } // end anonymous namespace
    
    //----------------------------------------------------------------------
    ReOrderColProxyModel::ReOrderColProxyModel(QObject *parent)
        :QIdentityProxyModel(parent)
    {}
    
    //----------------------------------------------------------------------
    QVariant ReOrderColProxyModel::data(const QModelIndex &proxyIndex, int role) const
    {
        int row= proxyIndex.row();
        int col= proxyIndex.column();
        QModelIndex ix= sourceModel()->index(row,static_cast<int>(FORWARD_LOOKUP[col]));
        return ix.data(role);
    }
    
    //----------------------------------------------------------------------
    QVariant ReOrderColProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        return sourceModel()->headerData(static_cast<int>(FORWARD_LOOKUP[section]),orientation,role);
    }
    
    //----------------------------------------------------------------------
    int ReOrderColProxyModel::columnCount(const QModelIndex &) const
    {
        return REORDER_FORWARD_LOOKUP_SIZE;
    }
    

    REPAIR_STATUS_COLUMN is an enumeration defined in constants.h. I'm sure that you can adapt it to your needs.

    When I was looking for a solution to my problem, @VRonin suggested I visit the KItemModels site at https://api.kde.org/frameworks/kitemmodels/html/classKRearrangeColumnsProxyModel.html

    I didn''t use their code which is more general than mine as I didn't need all that. Still their code is well worth looking at.

    Mike



  • Thank you. All of your answered helped to figure it out.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.