Megatron300
Megatron300

Reputation: 239

Selected effect around an item in QTreeWidget

The "selected effect" (the one you get when you use TAB) is always present in my QTreeWidget when I set the selection mode to ExtendSelection (I need it to be able to select multiple items).

The effect is gone if I remove this line from the code :

 ui->treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);

How can I remove it while keeping the ExetendSelection? Here is the picture. (To be clear, what I don't want is the border effect around the item "Amis")

Example

Thanks.

Upvotes: 0

Views: 242

Answers (1)

Tomas
Tomas

Reputation: 2210

As SaZ said, you have to use a custom delegate with the overridden paint() method.

In my projects I use this approach:

void CMyDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QStyleOptionViewItem itemOption(option);

    // This solves your problem - it removes a "focus rectangle".
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;

    initStyleOption(&itemOption, index);

    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
}

In the previous example, CMyDelegate was derived from the QStyledItemDelegate. You can also derive from the QItemDelegate and your paint() method will look like this:

void CMyDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QStyleOptionViewItem itemOption(option);

    // This solves your problem - it removes a "focus rectangle".
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;

    QItemDelegate::paint(painter, itemOption, index);
}

And this is how to use the custom delegate:

CMyDelegate* delegate = new CMyDelegate(treeWidget);
treeWidget->setItemDelegate(delegate);

Upvotes: 1

Related Questions