PDA

View Full Version : QAbstractTableModel::flags -> The view has a checkbox?



kroenecker
24th June 2008, 18:26
I've implemented the followings flags function for a custom model. For some reason, the view has a checkbox in each item in the view. Any ideas why?



Qt::ItemFlags ConstantsModel::flags(const QModelIndex &index)const
{
if(index.isValid())
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;

return Qt::NoItemFlags;
}

kroenecker
24th June 2008, 20:00
I'm still not sure what the problem is.

Here is my data method. When I use the commented code, the cells don't have checkboxes (but input data doesn't stick). When I use the code as it is below, the data sticks, but one checkbox is in each index.



QVariant ConstantsModel::data(const QModelIndex &index, int role) const
{
if(index.column() == 0)
//return QVariant(ConstantValues->at(index.row()));
return QVariant(ConstantValues->at(index.row()).X);

if(index.column() == 1)
//return QVariant(ConstantValues->at(index.row()));
return QVariant(ConstantValues->at(index.row()).Y);

return QVariant();
}


Here is my setData method (if this helps any):


bool ConstantsModel::setData(const QModelIndex &index, const QVariant &Value, int role)
{
ConstantsModelPoint point = ConstantValues->takeAt(index.row());

if(index.column() == 0)
{
point.X = Value.toString();
ConstantValues->insert(index.row(), point);
emit this->layoutChanged();
return true;
}
if(index.column() == 1)
{
point.Y = Value.toDouble();
ConstantValues->insert(index.row(), point);
emit this->layoutChanged();
return true;
}
return false;
}


Finally the data container "ConstantValues" is a QList containing elements from this class:



class ConstantsModelPoint : public QVariant
{
public:
ConstantsModelPoint();
~ConstantsModelPoint();

QString X;
double Y;
};

kroenecker
24th June 2008, 20:16
I figured it out:



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

if (role == Qt::DisplayRole) {
if (index.column() == 0)
return QVariant(ConstantValues->at(index.row()).X);

if (index.column() == 1)
return QVariant(QString::number(ConstantValues->at(index.row()).Y));
}
return QVariant();
}


I wasn't checking for the Qt::DisplayRole. Somehow the QString and the double were being interpreted as asking for a checkbox. I don't know exactly, but this has fixed things.

fullmetalcoder
24th June 2008, 21:10
Read the docs ;). The Qt::ItemDataRole (http://doc.trolltech.com/4.3/qt.html#ItemDataRole-enum) enum has a value named Qt::CheckStateRole. If the value returned is not false (when converted to bool from QVariant) then a checkbox is displayed...