Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Retrieve current font from model



  • 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...


  • Moderators

    @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.



  • @J-Hilk Got it. I didn't know that declaring QFont font; was enough to set it as the current font of the view!


Log in to reply