【问题标题】:Selected effect around an item in QTreeWidgetQTreeWidget中项目周围的选定效果
【发布时间】:2016-03-28 20:25:18
【问题描述】:

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

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

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

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

Example

谢谢。

【问题讨论】:

  • 您需要使用自定义委托,该委托将覆盖paint 方法。

标签: c++ qt


【解决方案1】:

正如 SaZ 所说,您必须使用自定义 delegate 并覆盖 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多