Solved Delegate the QtableView colors
-
Hi Guys!
I have a QtableView with QSqlTableModel model. I would like to achive different color rows and the default celector's frame disappear. I found on the internet some solution for these problems, but i cant use these solutions in the same time.
This is for different row colors:class ColorDelegate: public QItemDelegate { public: QColor color; ColorDelegate(QColor colo,QObject *parent = 0) : QItemDelegate(parent) {color = colo;} public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { drawBackground(painter, option, index); QItemDelegate::paint(painter, option, index); } protected: virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); painter->fillRect(option.rect, color); } };
and usage:
for (int i = 0;i<model->rowCount();i++) { if(i%2==0) view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view)); else view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view)); }
And this is for default non default select (i want to keep the keyboard navigation, so this solution was the good for it)
class NoFocusDelegate : public QStyledItemDelegate { protected: void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; }; void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const { QStyleOptionViewItem itemOption(option); if (itemOption.state & QStyle::State_HasFocus) itemOption.state = itemOption.state ^ QStyle::State_HasFocus; QStyledItemDelegate::paint(painter, itemOption, index); };
and usage:
view->setItemDelegate(new NoFocusDelegate());
Any ideas to to achieve both? Thanks! (And sorry for my bad english again!)
-
@Kaguro
ah my bad. we used the wrong option here
QStyledItemDelegate::paint(painter, option, index);
should be
QStyledItemDelegate::paint(painter, itemOption, index); -
Hi
Should not be an issue to combine to one.
QStyledItemDelegate is the replacement for QItemDelegate
So you can just do merge the 2 delegates to one. -
@mrjj hmm, and how can i merge to one? Can i get an example? Cuse when i tried it i did something wrong :(
-
@Kaguro
Hi
Likeclass ColorDelegate: public QStyledItemDelegate { public: QColor color; ColorDelegate(QColor colo, QObject *parent = 0) : QStyledItemDelegate(parent) { color = colo; } public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { drawBackground(painter, option, index); QStyleOptionViewItem itemOption(option); if (itemOption.state & QStyle::State_HasFocus) itemOption.state = itemOption.state ^ QStyle::State_HasFocus; QStyledItemDelegate::paint(painter, option, index); } protected: virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); painter->fillRect(option.rect, color); } };
-
@mrjj If i use it this way like your suggestion:
for (int i = 0;i<model->rowCount();i++) { if(i%2==0) view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view)); else view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view)); }
the rows repaint is working but the default select frame is not disapear :(
-
@Kaguro
ah my bad. we used the wrong option here
QStyledItemDelegate::paint(painter, option, index);
should be
QStyledItemDelegate::paint(painter, itemOption, index); -
@mrjj and it works! You are my hero! :D