{SOLVED} How to change the border color of QTableWidgetItem
-
Hi,
We have a requirement where we want to change the border color of the QTableWidget item to pink when that item is clicked or selected.
I tried implementing QItemDelegate / QStyledItemDelegate but it doesn't work.
@class MyDelegate : public QStyledItemDelegate {
public:
MyDelegate( QObject *parent ) : QStyledItemDelegate( parent )
{ }void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const { QStyledItemDelegate::initStyleOption(option, index); } void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { QStyledItemDelegate::paint( painter, option, index ); //if( option.state == QStyle::State_HasFocus ) { QPen pen(Qt::red); pen.setWidth(1); pen.setStyle(Qt::SolidLine); painter->setPen( pen ); painter->drawRect( option.rect ); } }
};@
@:
:
:myTableWidget->setItemDelegate(new MyDelegate());@
Please let me know if I am missing something.
Regards,
Vrushali -
- please use code tags (surrounding with @-sign)
- what doesn't work
- dumb question: did you set the delegate on the view?
- your delegate implementation doesn't paint anything except the border?! So no item content
-
Hi,
Thanks for pointing that out. Updated post with appropriate code tags.
It doesn't work as in doesn't show any border around the item.
I have set the delegate on QTableWidget as follows:
@myTableWidget->setItemDelegate(new MyDelegate());@
in the delegate implementation the default method is called, so wont it take care of other content?
@QStyledItemDelegate::paint( painter, option, index );@
-
[quote author="vrushali_deshpande" date="1413892856"]Hi,
in the delegate implementation the default method is called, so wont it take care of other content?
@QStyledItemDelegate::paint( painter, option, index );@
[/quote]
thats why i asked you to add the code-tags. I overread that line ;)Since you want to replace the focus frame do this:
@
void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
bool hasFocus = (option.state & QStyle::State_HasFocus);
option.state &= ~QStyle::State_HasFocus;
QStyledItemDelegate::paint( painter, option, index );if( hasFocus ) { QRect focusRect = style->subElementRect(QStyle::SE_ItemViewItemFocusRect, &option, option.widget); painter->save(); painter->setBrush( Qt::NoBrush ); QPen pen(Qt::red); pen.setWidth(1); pen.setStyle(Qt::SolidLine); painter->setPen( pen ); painter->drawRect( focusRect ); painter->restore(); }
}
@ -
Hi,
Thanks for the solution. Its working now :)