I use a QTableWidget with two columns.
The first column display images. The second column display text.
I'm calling QBrush::setTexture
on the first column, not the setIcon
function.
I want the first column images to change when I click on their cell but there is a problem with the image that appears when I click the cell.
This is the image that I expect:
This is the image that I get:
Here is the code
.h file
class KcBackgroundDelegate : public QStyledItemDelegate { public:
explicit KcBackgroundDelegate(QObject *parent = 0)
: QStyledItemDelegate(parent)
{
}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QVariant background = index.data(Qt::BackgroundRole);
if (background.canConvert<QBrush>())
painter->fillRect(option.rect, background.value<QBrush>());
QStyledItemDelegate::paint(painter, option, index);
if(option.state & QStyle::State_MouseOver)
{
if(index.column() == 0 )
{
painter->save();
painter->fillRect(option.rect,QBrush(QPixmap(":/Resources/img/System/setting_aircraft_on_bt.png")));
painter->restore();
}
}
if(option.state & QStyle::State_Selected)
{
if(index.column() == 0)
{
painter->save();
painter->fillRect(option.rect,QBrush(QPixmap(":/Resources/img/System/setting_aircraft_click_bt.png")));
painter->restore();
}
}
}
};
.cpp file
ui->TW_AIRCRAFT_TYPE->setItemDelegateForColumn(0,new KcBackgroundDelegate(this));
ui->TW_AIRCRAFT_TYPE->setMouseTracking(true);
ui->TW_AIRCRAFT_TYPE->viewport()->setMouseTracking(true);
ui->TW_AIRCRAFT_TYPE->installEventFilter(this);
ui->TW_AIRCRAFT_TYPE->viewport()->installEventFilter(this);
It seems there is a problem with the delegate but I don't know how to fix it. Please help me.