QTreeWidget中项目周围的选定效果

时间:2016-03-28 20:25:19

标签: c++ qt

当我将选择模式设置为ExtendSelection(我需要它能够选择多个项目)时,我的QTreeWidget中始终存在“选择的效果”(当您使用TAB时获得的效果)。

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

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

如何在保持ExetendSelection的同时删除它?这是图片。 (要清楚,我不想要的是“Amis”项目周围的边界效应)

Example

感谢。

1 个答案:

答案 0 :(得分:1)

正如 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);
相关问题