QTreeWidget中项目周围的选定效果

Selected effect around an item in QTreeWidget

本文关键字:项目 周围 QTreeWidget      更新时间:2023-10-16

当我将选择模式设置为ExtendeSelection(我需要它能够选择多个项目)时,"所选效果"(使用TAB时得到的效果)总是出现在我的QTreeWidget中。

如果我从代码中删除这一行,效果就消失了:

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

如何在保留ExetendSelection的同时删除它?这是照片。(需要明确的是,我不想要的是"Amis"项目周围的边界效应)

示例

谢谢。

正如SaZ所说,您必须使用带有重写paint()方法的自定义委托

在我的项目中,我使用这种方法:

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);
}

在前面的示例中,CMyDelegate是从QStyledItemDelegate导出的。您也可以从QItemDelegate派生,paint()方法如下所示:

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);
}

这就是如何使用自定义委托:

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