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. Qtableview aligment
Forum Update on Monday, May 27th 2025

Qtableview aligment

Scheduled Pinned Locked Moved Solved General and Desktop
qtableviewalign
5 Posts 2 Posters 3.8k Views
  • 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.
  • P Offline
    P Offline
    PabloArg
    wrote on 16 Aug 2016, 14:51 last edited by
    #1

    Somebody can give me a hand.
    I was trying to align my qtableview created as follow:
    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("SELECT * FROM SPSSourceLines");
    ui->tableViewSPSSourceLines->setModel(model);

    How Can I align all columns to the right side.

    Many Thanks

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 16 Aug 2016, 14:57 last edited by VRonin
      #2

      subclass QSqlQueryModel, reimplement virtual QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const
      with this body:

      QVariant MySQLModel::data(const QModelIndex &item, int role) const{
      if(role==Qt::TextAlignmentRole)
      return Qt::AlignRight | Qt::AlignVCenter;
      return QSqlQueryModel::data(item,role);
      }
      

      "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
      3
      • P Offline
        P Offline
        PabloArg
        wrote on 17 Aug 2016, 12:16 last edited by
        #3

        I create a Class called MyQSqlQueryModel.h and MyQSqlQueryModel.cpp,
        And I put the code that you suggect to me. And works fantastic, for the first column, then later I will do for the others.

        But I would like to know, if some columns the aligment is to right, other to center and other to left. Do I need to create a Subclass for each one, or I can manipulate with my method define in my MyQSqlQueryModel.

        Thanks Again.

        See the code below.

        ============ MyQSqlQueryModel.h =========================
        #ifndef MYQSQLQUERYMODEL_H
        #define MYQSQLQUERYMODEL_H
        #include <QSqlQueryModel>

        #include <QObject>

        class MyQSqlQueryModel : public QSqlQueryModel
        {
        public:
        MyQSqlQueryModel();
        QVariant data(const QModelIndex &item, int role) const;
        };

        #endif // MYQSQLQUERYMODEL_H

        ============ MyQSqlQueryModel.cpp =========================
        #include "myqsqlquerymodel.h"

        MyQSqlQueryModel::MyQSqlQueryModel()
        {

        }

        QVariant MyQSqlQueryModel::data(const QModelIndex &item, int role) const
        {
        if(role==Qt::TextAlignmentRole)
        return int(Qt::AlignRight | Qt::AlignVCenter);
        return QSqlQueryModel::data(item,role);
        }

        ============ Main Program ==================
        MyQSqlQueryModel *model = new MyQSqlQueryModel;
        model->setQuery("SELECT * FROM SPSSourceLines");
        QModelIndex index = model->index(0, 0);
        model->data(index,7);

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 17 Aug 2016, 12:32 last edited by
          #4

          if you want to asign the alignment by column just add an if in the data method. for example, to align the first column to the right, the second to the left and the rest in the middle use:

          QVariant MyQSqlQueryModel::data(const QModelIndex &item, int role) const
          {
          if(role==Qt::TextAlignmentRole){
          if(index.column()==0)
          return int(Qt::AlignRight | Qt::AlignVCenter);
          if(index.column()==1)
          return int(Qt::AlignLeft | Qt::AlignVCenter);
          return Qt::AlignCenter;
          }
          return QSqlQueryModel::data(item,role);
          }
          

          if you need to do it by cell then do not subclass QSqlQueryModel at all, just call something like:

          model->setData(model->index(row,column),int(Qt::AlignRight | Qt::AlignVCenter),Qt::TextAlignmentRole);
          

          "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
          • P Offline
            P Offline
            PabloArg
            wrote on 17 Aug 2016, 13:13 last edited by
            #5

            Hi again Vronin;

            I improve little bit your idea. Perphaps is usefull for somebody.
            As I comment before, my tableview will have diferent types of Aligment. Some columns goes to right, other left, other center.
            But, I will use the same subclass created for QSqlQueryModel I will call this subclass many times in diferent part of my application.
            I created something more variable, so at the moment that you call the subclass you must define by QStringList define in my subclass, for each column what kind of aligment you like.

            Thanks Vronin for your help. And I share the code with you

            ================== MyQSqlQueryModel.h ======================
            #ifndef MYQSQLQUERYMODEL_H
            #define MYQSQLQUERYMODEL_H
            #include <QSqlQueryModel>

            #include <QObject>

            class MyQSqlQueryModel : public QSqlQueryModel
            {
            public:
            MyQSqlQueryModel();
            QVariant data(const QModelIndex &item, int role) const;
            QStringList AligmentDefinition; // HERE I DEFINE THE ALIGMENT BY COLUMNS
            };

            #endif // MYQSQLQUERYMODEL_H

            ================== MyQSqlQueryModel.cpp ======================
            #include "myqsqlquerymodel.h"

            MyQSqlQueryModel::MyQSqlQueryModel()
            {

            }

            QVariant MyQSqlQueryModel::data(const QModelIndex &item, int role) const
            {
            if(role==Qt::TextAlignmentRole)
            {
            if(AligmentDefinition.size()==0)
            {// There are any definition
            return 0;
            }
            if(item.column() > AligmentDefinition.size())
            {// There are no definition for that Column
            return 0;
            }
            int i = item.column();
            QString Value = AligmentDefinition.value(i);
            if(Value =="1")
            return int(Qt::AlignRight | Qt::AlignVCenter);
            if(Value=="2")
            return int(Qt::AlignCenter | Qt::AlignVCenter);
            if(Value=="3")
            return int(Qt::AlignLeft | Qt::AlignVCenter);
            }
            return QSqlQueryModel::data(item,role);
            }

            =============== MAIN PROGRAM =======================
            MyQSqlQueryModel *model = new MyQSqlQueryModel;
            model->setQuery("SELECT * FROM SPSSourceLines");
            model->AligmentDefinition << "1" << "2" << "3";
            QModelIndex index;
            index = model->index(0,0);
            model->data(index,7);

            1 Reply Last reply
            0

            4/5

            17 Aug 2016, 12:32

            • Login

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