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] QStyledItemDelegate highlighting on focus/lost focus (Windows)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QStyledItemDelegate highlighting on focus/lost focus (Windows)

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.7k 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.
  • P Offline
    P Offline
    puterk
    wrote on last edited by
    #1

    Hello,

    I have a QStyledItemDelegate for formatting numbers on a tableview. I reimplemented the paint method to format the numbers and highlight the columns when the row is selected (the selection behavior of the table view is to select the whole row). The problem is that the columns with the delegate are highlighted blue when the tableview loses focus and the other columns with no delegate are highlighted gray. I don't know how to change the highlighting for the columns with delegates when the tableview loses focus.

    I found this on the QStyle documentation but i don't know what to do with it:
    @QStyle::SH_ItemView_ChangeHighlightOnFocus 22 Gray out selected items when losing focus.@

    Here's what it looks like:
    ![IMG]http://i60.tinypic.com/16022ib.jpg[/IMG](Has focus)!

    ![IMG]http://i61.tinypic.com/aorokw.jpg[/IMG](Lost focus)!

    Here is the .h of the delegate:
    @#ifndef NUMBERFORMATDELEGATE_H
    #define NUMBERFORMATDELEGATE_H

    #include <QStyledItemDelegate>

    class NumberFormatDelegate : public QStyledItemDelegate
    {
    Q_OBJECT
    public:
    explicit NumberFormatDelegate(QObject *parent = 0);
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    signals:

    public slots:

    };

    #endif // NUMBERFORMATDELEGATE_H@

    Here is the cpp:
    @#include "numberformatdelegate.h"
    #include <QPainter>

    NumberFormatDelegate::NumberFormatDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
    {
    }

    void NumberFormatDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QString text = index.model()->data(index, Qt::DisplayRole).toString();
    QStyleOptionViewItem myOption = option;
    QLocale ph(QLocale::English, QLocale::Philippines);
    QString formattedText = ph.toString(text.toDouble(), 'f', 2);

    if (myOption.state & QStyle::State_Selected)
    {
        painter->setPen(Qt::white);        
        painter->fillRect(option.rect, option.palette.highlight());
    }
    else
    {
        painter->setPen(QPen(option.palette.foreground(), 0));
        painter->setBrush(qvariant_cast<QBrush>(index.data(Qt::ForegroundRole)));
    }
    
    myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
    painter->drawText(myOption.rect, myOption.displayAlignment, formattedText);
    

    }@

    This only happens on windows. This is a minor issue for me but I hope someone could help me fix it. Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      IIRC, you also have to check the active state in order select the correct palette values

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        puterk
        wrote on last edited by
        #3

        Hello,

        Thanks for the help.

        Here is what I did:

        @void NumberFormatDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        QString text = index.model()->data(index, Qt::DisplayRole).toString();
        QStyleOptionViewItem myOption = option;
        QLocale ph(QLocale::English, QLocale::Philippines);
        QString formattedText = ph.toString(text.toDouble(), 'f', 2);

        if ((myOption.state & QStyle::State_Selected) && (myOption.state & QStyle::State_Active))
        {
            painter->setPen(Qt::white);        
            painter->fillRect(option.rect, option.palette.highlight());
        }
        else if((myOption.state & QStyle::State_Selected) && !(myOption.state & QStyle::State_HasFocus))
        {
            painter->fillRect(option.rect, option.palette.background());
        }
        else
        {
            painter->setPen(QPen(option.palette.foreground(), 0));
            painter->setBrush(qvariant_cast<QBrush>(index.data(Qt::ForegroundRole)));
        }
        
        myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
        painter->drawText(myOption.rect, myOption.displayAlignment, formattedText);
        

        }@

        It works but if there is much simpler way, I'd like to know.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If it's only a question of formatting the visible text, just overwrite displayText and remove your paint method.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            puterk
            wrote on last edited by
            #5

            Yeah that's what i did at first but the highlighting was weird so i re-implemented the paint method. It works fine now so thanks.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Strange, it should not have any influence.

              Another thing you can do is use a QIdentityProxyModel and modify your data there

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                puterk
                wrote on last edited by
                #7

                Yeah, The weirdness in the highlighting only occurs in Windows. In Linux it doesn't have any effect on the highlighting, I'll check out QIdentityProxyModel but I'll stick with the delegate for now.

                Thanks again!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then you might have found a bug. You should check the "bug report system":http://bugreports.qt-project.org to see if it's something known. If not please consider opening a new report providing a minimal compilable example that reproduce the behavior

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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 Extensions
                  • Unsolved