Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] QListview - alter font of one line dynamically

    General and Desktop
    2
    3
    2214
    Loading More Posts
    • 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
      Moschops last edited by

      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 Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        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 Reply Quote 0
        • M
          Moschops last edited by

          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 Reply Quote 0
          • First post
            Last post