Align right a column of QTableView



  • 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



  • 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!



  • 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



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


Log in to reply
 

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