QTreeWidget中项目周围的选定效果
Selected effect around an item in QTreeWidget
当我将选择模式设置为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);
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 如何禁用ListView选择视觉效果并绘制项目周围的矩形
- QTreeWidget中项目周围的选定效果