PDA

View Full Version : QTableview data display font question



MarkoSan
12th May 2008, 17:02
How to change qtableview data display font?

jpn
12th May 2008, 19:28
Use QWidget::setFont() or make your model return the desired font for Qt::FontRole.

MarkoSan
14th May 2008, 08:40
Use QWidget::setFont() (http://doc.trolltech.com/latest/qwidget.html#setFont) or make your model return the desired font for Qt::FontRole.

jpn, I've set the font using setFont(), but now the cell does not resize automaticly!!! How do I resize a cell acoording to font size? Do you have maybe some example?

jpn
14th May 2008, 08:46
What about the other solution?

MarkoSan
14th May 2008, 10:51
I have no idea how to achieve that ... :confused:

jpn
14th May 2008, 11:45
You said you have QTableView so you have a model too, right? QAbstractItemModel::data() returns data for various roles. Qt::FontRole is one of them.

MarkoSan
14th May 2008, 12:02
I have:


class, subclassed from QTableView (my view)
class, subclassed from QStandardItemModel (my model)
class, subclassed from QItemDelegate (my delegate)

How do I get to this font role?

jpn
14th May 2008, 12:50
QVariant MyCustomModel::data(const QModelIndex& index, int role) const
{
if (index.isValid() && role == Qt::FontRole)
return QFont(...);

return QStandardItemModel::data(index, role);
}

MarkoSan
14th May 2008, 12:57
Well, this is my code for data() function:
QVariant CShoppingCartModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && role==Qt::EditRole)
return m_pMerchandizeOrder->orders()->at(index.row()).strDisplayString;
//QStandardItemModel::data(index, role);
if(index.isValid() && role==Qt::FontRole)
return QFont("Courier New", 24, QFont::Bold);
return QVariant(); // returns invalid record
}

And the result is still the same, the cell size is not updated. What is still missing?