Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user editable checkbox in Qtableview

Tags:

qt

qt4

I want to implement a User editable checkbox in QTableView which is created using QAbstractModel. I am able to assign a checked and unchecked Checkbox but unable to make it editable. flag is set to QItemIsUserCheckable.

like image 216
user2228455 Avatar asked Apr 02 '13 05:04

user2228455


1 Answers

You can do it easily by implementing model's setData() method like this:

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;
}

And don't forget about your model's data() method:

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
    }
    //...
}
like image 87
SpongeBobFan Avatar answered Oct 05 '22 20:10

SpongeBobFan