Retrieve current font from model
Solved
General and Desktop
-
In my model I want to change the font in some particular cells, hence I'm going to use the
Qt::FontRole
:QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= _items.count()) return QVariant(); item_t item = _items.at(index.row()); switch (role) { case Qt::FontRole: { auto font = ???->font(); // how to retrieve the current font? font.setBold(true); return font; } } // ... }
But I'm not able to retrieve the current font to change it. Of course I can create a new one, but it would break any other settings...
-
In my model I want to change the font in some particular cells, hence I'm going to use the
Qt::FontRole
:QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= _items.count()) return QVariant(); item_t item = _items.at(index.row()); switch (role) { case Qt::FontRole: { auto font = ???->font(); // how to retrieve the current font? font.setBold(true); return font; } } // ... }
But I'm not able to retrieve the current font to change it. Of course I can create a new one, but it would break any other settings...
@Mark81 I don't think this is how this works, The View will request the font it's going to use via FontRole. If you donÄt specify a Font than the default one
QFont f
will be used. You''have to decide inside the data function when to set it ti bold and when not.