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

前端 未结 3 1249
抹茶落季
抹茶落季 2020-12-09 02:48

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

3条回答
  •  隐瞒了意图╮
    2020-12-09 03:22

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

提交回复
热议问题