当我设置了QAbstractItemModel可选但未启用的标志时,我不能通过鼠标点击来选择项目。但是,select()函数在内部选择对象。这是qt bug,还是我做错了什么?
发布于 2012-02-15 23:57:59
据我所知,您希望“禁用”该项目,但同时又可以选择它。在模型上伪造这一点相当容易。
if ( role == Qt::BackgroundRole ){
return QVariant(QApplication::palette()->color(QPalette::Inactive, QPalette::Window );
}这会将您的项目绘制为灰色,并且您仍然可以选择它。
发布于 2012-02-15 20:46:16
你做错了什么。如果您禁用一个小部件,它将灰显,并且它不接收用户的鼠标点击和键盘输入。
发布于 2015-11-10 21:24:23
我只是遇到了类似的问题(我需要复制禁用的项目)。以下是为禁用项设置正确样式的解决方案(不忽略任何样式表)。
为您的模型创建自定义项委托。
/// 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);https://stackoverflow.com/questions/9293308
复制相似问题