Qtableview aligment



  • 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



  • 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);
    }
    


  • 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);



  • 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);
    


  • 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);


Log in to reply
 

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