正如 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);