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

前端 未结 3 1247
抹茶落季
抹茶落季 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:23

    Following works for me:
    http://doc.qt.io/qt-5/qstandarditem.html#setTextAlignment

    void MainWindow::fillTable()
    {
        sudukoItem->setText( "qq" );
        sudukoItem->setTextAlignment(Qt::AlignCenter);
    
        sudukoModel->appendRow( sudukoItem );
        sudukoTable->setModel( sudukoModel );
    
        sudukoTable->setRowHeight( ( sudukoModel->rowCount() - 1 ), 100 );
        sudukoTable->setColumnWidth( ( sudukoModel->columnCount() - 1 ), 100 );
    }
    

    where:

    QTableView*         sudukoTable;
    QStandardItemModel* sudukoModel;
    QModelIndex*        modelIndex;
    
    QStandardItem*      sudukoItem;
    

    Credit goes to this comment: How to set text alignment on a column of QTableView programmatically?

    `item->setTextAlignment(Qt::AlignCenter); work well for me. – Ratah

提交回复
热议问题