Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Retrieve current font from model
Qt 6.12 LTS is out! See what's new in the release blog

Retrieve current font from model

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 772 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

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

    J.HilkJ 1 Reply Last reply
    0
    • M Mark81

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

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved