Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing column order in QSqlTableModel

Changing column order in QSqlTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 2.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      KRearrangeColumnsProxyModel

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • G gabor53

        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.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        G 1 Reply Last reply
        2
        • raven-worxR raven-worx

          @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.

          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @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.

          raven-worxR 1 Reply Last reply
          0
          • Ni.SumiN Offline
            Ni.SumiN Offline
            Ni.Sumi
            wrote on last edited by
            #5

            This is the error Part..

            ui->tableView_Fix *parent() = Q_NULLPTR
            

            check this,

            QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget * parent = 0)
            
            1 Reply Last reply
            1
            • G gabor53

              @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.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @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.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              3
              • M Offline
                M Offline
                mjsurette
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                3
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved