how set QCheckBox in QAbstractItemModel?
Asked Answered
A

4

5

I have a model

class TreeModel : public QAbstractItemModel

which I populate with instances of my TreeItem excluding column==1. In column 1 I've created CheckBoxes:

QVariant TreeModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
        return QVariant();

    if (role != Qt::DisplayRole) {
        if (role == Qt::CheckStateRole) {
            if (index.column() == 1) {
                if (index.row() == 1) {
                    return Qt::Unchecked;
                } else
                    return Qt::Checked;
            }
        }
        return QVariant();
    }
    if (role == Qt::DisplayRole) {
        if (index.column() != 1) {
            TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
            return item->data(index.column());
        }
    }
    return QVariant();
  }

I can set these CheckBoxes statues to Qt::Checked or Qt::Unchecked but my problem is: I cannot change them later when they are clicked (however setData is called with appropriate index.column==1 and role==Qt::CheckStateRole). I have seen examples with ItemDelegate - only this seems to work. Is this true? Do I have to use delegate in this scenario?

Here is my setData() function:

bool TreeModel::setData(const QModelIndex & index, const QVariant & value,
        int role) {
    if (role==Qt::CheckStateRole && index.column() == 1) {
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer());

        //if (item->data(index.column()) == Qt::Checked)
        if (value == Qt::Checked){
            int i=1;
            //check->setCheckState(1,Qt::Checked); //SIGSEGV
        }else{
            //(item->data(index.column())) = Qt::Unchecked;
            int i=2;
            //check->setCheckState(1,Qt::Unchecked);
        }

        emit dataChanged(index, index);
        return true;
    }
    emit dataChanged(index, index);
    return true;;
}


Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |  Qt::ItemIsEditable;
  }
Absurd answered 22/7, 2013 at 11:10 Comment(0)
A
6

I didn't understand the Qt conception. You have to set data in setData() (store check state) and then populate model in data() with this new value being returned for checkbox, alike this:

setData()

bool TreeModel::setData(const QModelIndex & index, const QVariant & value, int role) {
    if (role == Qt::CheckStateRole && index.column() == 1) {
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer());

        if (value == Qt::Checked) {
            checkedState_ = Qt::Checked;
        }
        else {
            checkedState_ = Qt::Unchecked;
        }
    }
    emit dataChanged(index, index);
    return true;
}

data()

QVariant TreeModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
        return QVariant();

    if (role == Qt::CheckStateRole) {
        if (index.column() == 1) {
            return checkedState_;
        }
    }
    else if (role == Qt::DisplayRole) {
        if (index.column() != 1) {
            TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
            return item->data(index.column());
        }
    }
    return QVariant();
}  
Absurd answered 22/7, 2013 at 12:25 Comment(1)
thank you very much also. storing data in setData() was what I have missedAbsurd
C
2

It seems strange to me that you are returning always the same result for the data(CheckStateRole)

 if (role == Qt::CheckStateRole) {
            if (index.column() == 1) {
                if (index.row() == 1) {
                    return Qt::Unchecked;
                } else
                    return Qt::Checked;
            }
        }

Unchecked for (1,1) and checked for ( distinct to 1 , 1)

setData() is being called, but then, when the view queries the model for a value, you return always the same in data()

Couperin answered 22/7, 2013 at 12:11 Comment(0)
A
1

Without any ItemDelegate, in the flags method you must return the flag: Qt::ItemIsUserCheckable.

For more information see: http://qt-project.org/doc/qt-4.8/qt.html#ItemFlag-enum

Augustaugusta answered 21/5, 2014 at 14:6 Comment(0)
D
0

Using examples from @Trompa and @tinky_winky i managed to create this working example, as @Trompa suggested we need to return a different value or store this value and retrieve it in our data() to get actual state at that particular index.

QVector<int> status;
int SData::getStatus(int pos)
{
    return status.at(pos);
}

void SData::setStatus(int pos,int value)
{
    status.insert(pos, value);
}
QVariant STableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DisplayRole)
        return rowList[index.row()][index.column()];
    else if (role == Qt::CheckStateRole && index.column() == 0)
    {

        int status = SData::instance().getStatus(index.row());
        if (status)
            return Qt::Checked;
        else
            return Qt::Unchecked;
    }
    else
        return QVariant();
}

bool STableModel::setData(const QModelIndex &index,
                         const QVariant &value, int role)
{
    if (!index.isValid() /*|| role != Qt::EditRole*/)
        return false;
    if (role == Qt::CheckStateRole)
    {
        if ((Qt::CheckState)value.toInt() == Qt::Checked)
        {
            SData::instance().setStatus(index.row(),1);
            return true;
        }
        else
        {
            SData::instance().setStatus(index.row(), 0);
            return true;
        }
    }
Doorman answered 29/1, 2016 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.