当我将 QAbstractItemModel 的标志设置为可选择但未启用时,我无法通过鼠标单击来选择项目。但是在内部 select() 函数选择对象。这是 qt 错误,还是我做错了什么?
问问题
1518 次
3 回答
3
据我了解,您想“禁用”该项目,但同时能够选择它。在模型上伪造它相当容易。
if ( role == Qt::BackgroundRole ){
return QVariant(QApplication::palette()->color(QPalette::Inactive, QPalette::Window );
}
这会将您的项目涂成灰色,您仍然可以选择它。
于 2012-02-15T15:57:58.833 回答
2
你做错了什么。如果您禁用一个小部件,它会显示为灰色,并且它不会接收用户的鼠标点击和键盘输入。
于 2012-02-15T12:46:15.557 回答
0
我只是遇到了类似的问题(我需要复制禁用的项目)。这是为禁用项目设置正确样式的解决方案(不忽略任何样式表)。
为您的模型创建自定义项目委托。
/// Returns false only if item needs to be rendered as disabled.
bool isIndexEnabled(const QModelIndex &index)
{
// Implement this function.
}
class ItemDelegate : public QStyledItemDelegate {
public:
explicit ItemDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent) {}
protected:
void initStyleOption(
QStyleOptionItemView *option, const QModelIndex &index) const override
{
QStyledItemDelegate::initStyleOption(option, index);
if (!isIndexEnabled(index))
option->state &= ~QStyle::State_Enabled;
}
};
将新项目委托设置为您的模型。
auto itemDelegate = new ItemDelegate(model)
model->setItemDelegate(itemDelegate);
于 2015-11-10T13:24:23.177 回答