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. Align right a column of QTableView
Forum Updated to NodeBB v4.3 + New Features

Align right a column of QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 4.2k 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.
  • K Offline
    K Offline
    Kamar
    wrote on last edited by
    #1

    Hi,

    First thing, sorry for my bad english and i hope you'll understand my issue;
    I am newbie in QT and I'm really stuck that's why I ask you to help me
    I have a Qtableview in Logista.cpp which I fill like this:

    QSqlQueryModel * modalStockRec= new QSqlQueryModel();
            QSqlQuery QryTabStockRec;
            QryTabStockRec.exec("select codvar 'Variété',CAST(sum(pdsfru) as signed integer) as 'Tout-Venant', \
                if(frigo='N' and deverdi='N' and etat='R', cast(sum(pdsfru) as signed integer) ,0) as Arrivage, \
                if(frigo='O' and deverdi='N' and etat='F', cast(sum(pdsfru) as signed integer) ,0) as Frigo, \
                if(frigo='N' and deverdi='O' and etat='F', cast(sum(pdsfru) as signed integer),0)  as Deverdissage, \
                if((frigo='O' or deverdi='O') and etat='R', cast(sum(pdsfru) as signed integer),0 )  as Ressuyage \
                from palbrut where numver=0 group by 1;");
            modalStockRec->setQuery(QryTabStockRec);
            ui->TabStockRec->setModel(modalStockRec);
    

    I created a Class to align the QTableView
    stylingproxymodel.h :

    #ifndef STYLINGPROXYMODEL_H
    #define STYLINGPROXYMODEL_H
     
    #include <QObject>
    #include <QIdentityProxyModel>
    #include <QModelIndex>
    #include <QObject>
     
     
    class StylingProxyModel : public QIdentityProxyModel
    {
      QVariant data(const QModelIndex &index, int role) const
      {
        if (role != Qt::TextAlignmentRole)
          return QIdentityProxyModel::data(index, role);
     
        return Qt::AlignCenter;
      }
     
    public :
      StylingProxyModel();
    };
     
    #endif // STYLINGPROXYMODEL_H
    

    stylingproxymodel.cpp :

    #include "stylingproxymodel.h"
     
    StylingProxyModel::StylingProxyModel()
    {
     
    }
    

    I don't know if i do it right, if not correct me please
    Now i want to link this class with the QTableView
    Help me please :(
    Thank you

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bask185
      wrote on last edited by
      #2

      I'll post a code snippet of mine in which I set all kind of parameters. I should tell I am using 5 exact same table widgets called monitors

      for(QTableWidget* monitor : monitors){
             monitor->setRowCount(16);
             monitor->setColumnCount(40);
             monitor->setShowGrid(false);
             monitor->horizontalHeader()->hide(); // requires #include <QHeaderView>
             monitor->verticalHeader()->hide();
             monitor->rowHeight(5);
             monitor->columnWidth(5);
             monitor->horizontalHeader()->setDefaultSectionSize(23);
             monitor->verticalHeader()->setDefaultSectionSize(29);
             monitor->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
             monitor->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
             monitor->setDisabled(true);
         }
      
        for(int j = 0; j < 16; j++) {
             for(int k= 0; k < 40; k++) {
                 for(int i = 0; i < 5; i++) {
                     column[i] = k;
                     row[i] = j;
                     monitors[i]->model()->setData(monitors[i]->model()->index(row[i],column[i]),QBrush(Qt::green),Qt::ForegroundRole);
                     monitors[i]->item(row[i],column[i])->setTextAlignment(Qt::AlignCenter); // < alignment code can also AlignRight
                 }
            }
         }
      

      This code is situated in the setup of my Mainwindow.cpp file I did not make a separate class for it, so I hope you can find something usefulls in it. Good luck!

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        Given QSqlQueryModel is write only you need a proxy model on top of it. This one: https://pastebin.com/gmNCuUFk is a proxy model that adds a writeable layer on top of a flat (i.e. not a tree) model.

        now you can call setData(index,Qt::AlignCenter,Qt::TextAlignmentRole); to set it however you want it

        "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
        • K Offline
          K Offline
          Kamar
          wrote on last edited by
          #4

          @bask185 thank you but i don't use a QTableWidget :(
          @VRonin thank you a lot it helped me a lot

          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