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

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

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

提交回复
热议问题