You are not logged in.

1

Tuesday, April 24th 2012, 7:58am

Format values in a TableView

I'm accessing a PostgreSQL table and displaying records in a QTableView from a QSqlTableModel. For certain numerical values ​​greater than one million (4,357,254) shows me the value is 4.357e+06 in QTableView and I wish that would show the 4.357.254.

I also like to right align numeric values ​​and I get the default left-aligned

How I can access the format of these values?



Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
QString strSql="SELECT * FROM table"; 
QSqlQueryModel *model=new QSqlQueryModel(this); 
model->setQuery(strSql); 
model->setHeaderData(7, Qt::Horizontal, QObject::tr("Coord Y")); 
ui.tableView->setModel(model); 

int nRows= ui.tableView->verticalHeader()->count(); 
ui.tableView->setCurrentIndex(model->index(0,0)); 
for (int i=0;i<nRows;i++) 
{ 
if(ui.tableView->currentIndex().isValid()==true) 
{ 
QModelIndex index = model->index(i,7); 
QVariant y=model->data(index,Qt::EditRole); 
int coorY=y.toInt(); 
model->setData(index,coorY,Qt::EditRole); 
} 
ui.tableView->setCurrentIndex(model->index(i,0)); 
} 
}

pitonyak

Trainee

  • "pitonyak" is male

Posts: 58

Location: Columbus, Ohio, USA

  • Send private message

2

Tuesday, April 24th 2012, 8:49pm

Have you looked at using a QStyledItemDelegate? This is how I expected you to do this.