1 Attachment(s)
QAbstractItemModel derivative showing a checkbox next to each index
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:
Attachment 10366
See the ftp file listing to the middle-right portion of the screenshot.
Before you ask, the flags() method is this:
Code:
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:
Code:
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 :)
Re: QAbstractItemModel derivative showing a checkbox next to each index
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,
_
Re: QAbstractItemModel derivative showing a checkbox next to each index
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 :)
Re: QAbstractItemModel derivative showing a checkbox next to each index
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()
Quote:
Note: If you do not have a value to return, return an invalid QVariant instead of returning 0.
Cheers,
_
Re: QAbstractItemModel derivative showing a checkbox next to each index
Ok. Thanks. Now I think I fully got it. Some more checks are due now in my code :lol:
Thank you.