[SOLVED] QStyledItemDelegate highlighting on focus/lost focus (Windows)
-
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!
-
Hi,
IIRC, you also have to check the active state in order select the correct palette values
Hope it helps
-
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.
-
If it's only a question of formatting the visible text, just overwrite displayText and remove your paint method.
-
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.
-
Strange, it should not have any influence.
Another thing you can do is use a QIdentityProxyModel and modify your data there
-
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!
-
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