PDA

View Full Version : How to center text in a QTableView



dougbroadwell
23rd March 2009, 17:50
Qt 4.5.0

I'm trying to center the text in a QTableView, have been looking through the Deligate Classes, QItemDelegate Class and QStyledItemDelegate Class documentation, along with ItemData Role's and TextAlignmentRole but still cannot understand what exactly I need to do center the text in a column.

Thanks,
Doug

spirit
23rd March 2009, 17:56
did you try to do this


...
const QModelIndex index = table->model()->index(0, 0);
table->model()->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
table->model()->setData(index, "hello world");
...

?

dougbroadwell
23rd March 2009, 18:21
Nope, didn't work. Here's my code:


void MainWindow::SetupDetailWidget()
{
// Set up the ForecastDetail (database) table Model //

DetailModel = new QSqlRelationalTableModel(this);
DetailModel->setTable ("ForecastDetail");
DetailModel->setSort (Detail_LineNmb_Col, Qt::AscendingOrder);
DetailModel->setHeaderData(Detail_itemId_Col, Qt::Horizontal, "Item");
DetailModel->setHeaderData(Detail_Qua_Col, Qt::Horizontal, "Qua");

// Set up the ForecastDetail (database) table View //

ui.tvForecastDetails->setModel(DetailModel);
ui.tvForecastDetails->setColumnHidden(Detail_Agency_Col, true);
ui.tvForecastDetails->setColumnHidden(Detail_LineNmb_Col, true);
ui.tvForecastDetails->setSelectionMode (QAbstractItemView::SingleSelection);
ui.tvForecastDetails->setSelectionBehavior(QAbstractItemView::SelectRows );
ui.tvForecastDetails->setEditTriggers (QAbstractItemView::AllEditTriggers);
ui.tvForecastDetails->setColumnWidth(Detail_itemId_Col, 70);
ui.tvForecastDetails->setColumnWidth(Detail_Qua_Col, 50);
const QModelIndex index = ui.tvForecastDetails->model()->index(0, Detail_Qua_Col);
ui.tvForecastDetails->model()->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
}


The actual reading in if the data into the model is done elsewhere.

Thanks

faldzip
23rd March 2009, 19:25
I would make subclass of QSqlRelationalTableModel and reimplement data method like this:


QVariant MySqlRelationalModel::data ( const QModelIndex & index, int role ) const
{
if (role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
return QSqlRelationalTableModel::data(index, role);
}

Lykurg
23rd March 2009, 19:41
I would make subclass of QSqlRelationalTableModel and reimplement data

This code will center all colums. If you only want center a specific column you have to interpret index, and then the model is the "wrong" place for that. So better use a custom delegate. But normally such an easy and normal thing should work with Qt. Have you a minimal compilable example?

faldzip
23rd March 2009, 19:58
This code will center all colums. If you only want center a specific column you have to interpret index, and then the model is the "wrong" place for that. So better use a custom delegate. But normally such an easy and normal thing should work with Qt. Have you a minimal compilable example?
Of course, that the best way is to split data from it's presentation, and use some delegate for custom presentation, but, as there was no word about centering just one column, I thought that it would be the fastest way. :]

dougbroadwell
24th March 2009, 19:00
This is the simplified code I tried:


void MainWindow::SetupHeaderWidget()
{
HeaderModel = new QSqlTableModel(this);
HeaderModel->setTable ("ForecastHeader");
HeaderModel->select();
const QModelIndex index = HeaderModel->index(0, 0);
HeaderModel->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);


I would think that this would not work as I want the whole column 0 to be centered, not just row 0, but even row 0 column 0 is not centered. It doesn't matter if setting the TextAlignmentRole is done before or after the select()

Thanks
Doug

dougbroadwell
24th March 2009, 20:11
I tried subclassing QSqlTableModel and reimplementing data() as recommended above but that didn't work either.

spirit
25th March 2009, 06:12
in this case, create your own delegate and reimplement QItemDelegate::drawDisplay.

talk2amulya
26th March 2009, 11:17
perhaps u can try calling setData on model before you have called setModel() on the view..that might be the issue