[Solved] Text alignment in QTableView cell



  • Is there any way align data shown in cells of QTableWiev without reimplementing of model's methods?
    I mean some property like setAlign.



  • You can do these things from two sides: from the model side (including using a proxy model) or from the delegate side. All approaches unfortunately involve subclassing, unless you are using something like QStandardItemModel.



  • I actually use QStandardItemModel.



  • In that case, you can just call
    @
    myStandardItem->setData(Qt::AlignRight, Qt::TextAlignmentRole);
    @
    to align an item to the right side of the cell. Check the documentation on Qt::AlignmentFlag for other alignment options, and the Qt::ItemDataRole documentation for information on other interesting roles you may want to set.



  • Thanks a lot!
    That is what i needed.



  • [quote author="Andre" date="1322130004"]In that case, you can just call
    @
    myStandardItem->setData(Qt::AlignRight, Qt::TextAlignmentRole);
    @
    to align an item to the right side of the cell. Check the documentation on Qt::AlignmentFlag for other alignment options, and the Qt::ItemDataRole documentation for information on other interesting roles you may want to set.[/quote]

    I face same issue here but it didn't work with me in Qt5 :( check this snippet please:

    [code]void MainWindow::on_pushButton_clicked()
    {
    pDB = QSqlDatabase::addDatabase("QSQLITE");
    pDB.setDatabaseName("./data.db");
    if (pDB.open()){
    QSqlTableModel *pTableModel = new QSqlTableModel(0, pDB);
    pTableModel->setTable("Jornal");
    pTableModel->select();

       ui->tableView->setModel(pTableModel);
       ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
       // BUG: This is absolute error!!!
       model->setData(Qt::AlignRight, Qt::TextAlignmentRole);
    

    }
    else
    qDebug()<<"Error db";
    }[/code]

    PS
    I don't want to do any subclassing I'm looking for easy bezy solution



  • Hi!
    I think it's works only for QStandardItemModel. As Andre says:
    [quote author="Andre" date="1322127930"]You can do these things from two sides: from the model side (including using a proxy model) or from the delegate side. All approaches unfortunately involve subclassing, unless you are using something like QStandardItemModel. [/quote]
    But i am not using Qt 5 for now and maybe something changed there.



  • [quote author="mbnoimi" date="1366731763"]
    PS
    I don't want to do any subclassing I'm looking for easy bezy solution[/quote]
    By the way subclassing is not so terrifying as you can imagine at first time, after some practice it will be easy bezy and most convinient way.



  • bq. But i am not using Qt 5 for now and maybe something changed there.

    Could you please post a snippet explains the solution you've reached to in Qt4?

    bq. By the way subclassing is not so terrifying as you can imagine at first time, after some practice it will be easy bezy and most convinient way.

    I agree with you. subclassing not terrifying but for rapid developing and simple tools it force me to write more codes which means more time.

    A solution without subclassing will be more easy to write and simple to deal with.


  • Lifetime Qt Champion

    Sometimes you just can't avoid to subclass and this case, it really isn't that terrible.

    You can either subclass your model or use a "QIdentityProxyModel":http://qt-project.org/doc/qt-4.8/qidentityproxymodel.html#details

    @
    class DateFormatProxyModel : public QIdentityProxyModel
    {
    ..//Add constructor

    QVariant data(const QModelIndex &index, int role)
    {
    if (role == Qt::TextAlignmentRole) {
    return Qt::AlignRight;
    }
    return QIdentityProxyModel::data(index, role);
    }

    };@

    The same can be done subclassing the model you are using.

    Hope it helps



  • I came from the death heheheh:

    I tried to use QIdentityProxyModel as you mentioned above but my app unexpectedly finish!!!

    How can I use QIdentityProxyModel with QTableView?

    [code]#include <QIdentityProxyModel>

    class MyProxyModel : public QIdentityProxyModel
    {
    public:
    MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
    {
    qDebug() << "Do nothin!";
    }
    QVariant data(const QModelIndex &index, int role) const
    {
    if (role == Qt::TextAlignmentRole) {
    return Qt::AlignCenter;
    }
    return QIdentityProxyModel::data(index, role);
    }
    };[/code]

    Call it for QTableView:

    [code]
    QSqlTableModel *_tableModel = new QSqlTableModel(0, db);
    _tableModel->setTable(table);
    _tableModel->select();
    MyProxyModel *_proxy;
    _proxy->setSourceModel(_tableModel);
    ui->tableView->setModel(_proxy);
    [/code]


  • Lifetime Qt Champion

    You need to create it first:

    @MyProxyModel *_proxy = new MyProxyModel;@



  • [quote author="SGaist" date="1395262620"]MyProxyModel *_proxy = new MyProxyModel;[/quote]

    Didn't fix the issue... I got this error when I added it:

    [code]In file included from ../QTableViewRTL/mainwindow.h:10:0,
    from ../QTableViewRTL/mainwindow.cpp:1:
    ../QTableViewRTL/myproxymodel.h:9:40: error: ISO C++ forbids declaration of 'MyIdentityModel' with no type [-fpermissive]
    MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
    ^
    ../QTableViewRTL/myproxymodel.h: In member function 'int MyProxyModel::MyIdentityModel(QObject*)':
    ../QTableViewRTL/myproxymodel.h:9:43: error: only constructors take member initializers
    MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
    ^
    ../QTableViewRTL/myproxymodel.h:12:5: warning: no return statement in function returning non-void [-Wreturn-type]
    }
    ^
    make: *** [mainwindow.o] Error 1
    23:01:45: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project QTableViewRTL (kit: Desktop Qt 5.2.1 GCC 64bit)
    When executing step 'Make'
    23:01:45: Elapsed time: 00:02.[/code]


  • Lifetime Qt Champion

    It fixes your crashing issue.

    Now the compiler error is something else. Correct the constructor name, your class is called MyProxyModel and your are using MyIdentityModel as constructor name.



  • Oops my bad... Thank you.



  • Can i mark the thread as solved? :)



  • [quote author="qxoz" date="1395313861"]Can i mark the thread as solved? :)[/quote]

    Yes please.


Log in to reply
 

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