Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QListview - alter font of one line dynamically
Forum Updated to NodeBB v4.3 + New Features

[Solved] QListview - alter font of one line dynamically

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k 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
    Moschops
    wrote on 19 Nov 2013, 09:32 last edited by
    #1

    I have a QListView, happily showing the contents of a model. The user can select items, and onDoubleClick() makes things happen. It would be nice if the user had some extant way of knowing what they had double-clicked last given that they can change the selection with single-click after having done so, and an obvious way to do this is to make whatever line was last double-clicked bold.

    How can I do this? Am I going to have to subclass QListView and make my own paint function or some such?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 19 Nov 2013, 11:17 last edited by
      #2

      You would have to do the drawing of the text yourself in a delegate subclass.
      Try this and check if it fits your needs:
      @
      void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
      //draw background and selection-highlighting
      #if QT_VERSION < 0x050000
      QStyleOptionViewItemV4 opt(*qstyleoption_cast<const QStyleOptionViewItemV4 *>(&option));
      #else
      QStyleOptionViewItem opt = option;
      #endif

      this->initStyleOption(&opt, index);
      QString text = opt.text;
      opt.text = "";
      const QWidget *widget = opt.widget;
      QStyle *style = widget ? widget->style() : QApplication::style();
      style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
      
      QFont font = opt.font;
      if( ... )   //your condition to decide if you want to draw bold text or not
          font.setBold(true);
      
      painter->save();
          painter->setRenderHints(QPainter::TextAntialiasing);
          painter->setFont(font);
          painter->drawText( option.rect, text );
      painter->restore();
      

      }
      @
      Not tested though.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Moschops
        wrote on 19 Nov 2013, 13:07 last edited by
        #3

        Turned out my first approach there was massive overkill. In the end, it was simple:

        @QVariant subclassedModel::data(const QModelIndex& index, int role) const
        {
        ...

        if (role == Qt::FontRole)
        {
        
            if (condition_on_model_item_to_need_to_be_in_bold_font)
            {
                QFont boldFont;
                boldFont.setBold(true);
                return boldFont;
            }
           
        }@
        
        1 Reply Last reply
        0

        1/3

        19 Nov 2013, 09:32

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved