PDA

View Full Version : QAbstractItemModel derivative showing a checkbox next to each index



i92guboj
17th May 2014, 11:35
Hello.

I am creating an FtpFileSystemModel, and, for that, I am using QAbstractItemModel as a base, which I have never subclassed before.

After quite a few problems I have reached the point where my ftp files are displayed in a QTreeView, just like a QFileSystemModel would do with a local fs.

There are a few quirks here and there, though. The thing that worries me is that all the indexes (or "cells"), show a ticker next to them. This is better illustrated with a screenshot:

10366

See the ftp file listing to the middle-right portion of the screenshot.

Before you ask, the flags() method is this:


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

Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
qDebug() << Q_FUNC_INFO << QString("flags: %1").arg(flags);
return flags;
}

As you see, neither of Qt::ItemIsUserCheckable or Qt::ItemIsTristate is set.

I have also a problem with alignment, but I haven't dug much into it. I am setting these in data(), but they don't seem to do anything:



if(role == Qt::TextAlignmentRole)
{
Qt::Alignment flags = 0;

if(index.column() == 0 || index.column() == 1)
{
flags = Qt::AlignVCenter | Qt::AlignLeft;
}
else
{
flags = Qt::AlignVCenter | Qt::AlignRight;
}
}


Thanks for any idea you can share :)

anda_skoa
17th May 2014, 13:17
This is most likely unrelated to flags() or alignment, the most likely cause is that your data() method returns something other than a null QVariant when it is asked for the CheckStateRole.

A common mistake is to forget to check the "role" argument that is passed to data().

Cheers,
_

i92guboj
18th May 2014, 09:49
It worked like a charm. Thanks.

But.... out of curiosity, how are you supposed to know that? I can't see that info in the subclassing document for QAbstractItemModel not even in the internet...

Again, thank you :)

anda_skoa
18th May 2014, 11:28
The API contract of the data() method is that it returns a null variant for all indexes or roles that it does not have actual data for.

From QAbstractItemModel::data()




Note: If you do not have a value to return, return an invalid QVariant instead of returning 0.


Cheers,
_

i92guboj
18th May 2014, 14:24
Ok. Thanks. Now I think I fully got it. Some more checks are due now in my code :lol:

Thank you.