Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text alignment on a column of QTableView programmatically?

Tags:

c++

qt

qt4

So far the only solution I have found is to subclass QItemDelegate and implement my alignment rule in the paint() function. Is it really the simplest way?

I am using the C++ API.

like image 765
Ali Avatar asked Feb 10 '11 15:02

Ali


2 Answers

The alternative to subclussing QItemDelegate is to subclass your model and override data() method.

QVariant MyModel::data(const QModelIndex& index, int role) const {
    if (index.column() == yourCellIndex && role == Qt::TextAlignmentRole) {
        return Qt::AlignLeft;
    } else {
        return QVariant();
    }
}
like image 133
Nekuromento Avatar answered Nov 18 '22 17:11

Nekuromento


QSqlTableModel *model2= new QSqlTableModel();

model2->setTable("Save");
model2->select();

QSortFilterProxyModel *proxy1=new QSortFilterProxyModel();
proxy1->setSourceModel(model2);

QStandardItemModel *modd=new QStandardItemModel();

for (int z =0; z< proxy1->rowCount(); ++z)
   {
    for (int y =0; y< proxy1->columnCount(); ++y)
        {
        QStandardItem *item= new QStandardItem();
        item->setText(proxy1->index(z,y).data().toString());
        item->setTextAlignment(Qt::AlignCenter);
        modd->setItem(z,y,item);

        }
  }
ui->tableView->setModel(modd);
like image 3
J.Wayat Avatar answered Nov 18 '22 18:11

J.Wayat