1

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 expect

This is the image that I get:
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.

Cristik
  • 30,989
  • 25
  • 91
  • 127
minjee
  • 43
  • 1
  • 7
  • 1
    Why do you use `fillRect` with a brush created from the image, instead of just using `drawImage`? When a patterned brush is created from an image, I'm not sure if there are guaranties about the alignment of the pattern. – Karsten Koop Jun 02 '16 at 06:47
  • @KarstenKoop I didn't know because I do not use a lot of QPainter. I'll try. Thanks. – minjee Jun 03 '16 at 06:19

0 Answers0