Qableview中的用户可编辑复选框 [英] user editable checkbox in Qtableview

查看:21
本文介绍了Qableview中的用户可编辑复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在QTableView中实现一个用户可编辑的复选框,它是使用QAbstractModel创建的。我可以分配选中和取消选中的复选框,但无法使其可编辑。 标志设置为QItemIsUserCheckable

推荐答案

可以很容易地实现模型的setData()方法:

bool yourModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;
    if (role == Qt::CheckStateRole)
    {
        if ((Qt::CheckState)value.toInt() == Qt::Checked)
        {
            //user has checked item
            return true;
        }
        else
        {
            //user has unchecked item
            return true;
        }
    }
    return false;
}

别忘了模型的data()方法:

QVariant ProxyModelSubobjects::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    if (role == Qt::CheckStateRole && index.column() == COLUMN_WITH_CHECKBOX)
    {
        //return Qt::Checked or Qt::Unchecked here
    }
    //...
}

这篇关于Qableview中的用户可编辑复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆