Reimplementing QFileSystemModel Checkboxes using QMap inconsistent results
-
different checkboxes get checked like random even if there's nothing random
QVariant QCheckableFileSystemModel::data(const QModelIndex &index, int role) const{ ... return checked;
I am not a C++er, so this is either very right or very wrong! Are you supposed to return an
int
for aQVariant
? Because if not this could produce "random" state value for your checkboxes....[EDIT: Looks like my suggestion was very wrong, sigh :( ]
-
@mrjj said in Reimplementing QFileSystemModel Checkboxes using QMap inconsistent results:
@moffa13
Add some qDebug() statement to see what index are sent and what it set pr index.
I think its one of those cases where debugger and tools are more useful than guessing looking at code.If it was that simple I wouldn't have asked.
qDebug is showing me correct paths before calling setData. The problem is that for whatever reason, some checkboxes won't get checked. I almost always get a different result whenever I run this code.
qDebug in setData tells me that the function receives Qt::Checked but it's not the case.
-
Yeah, exactly, for me it should work.
However I noticed something which may be useful. I added a QEventLoop when doing setData to look for the dataChanged signal but when I do this and I use qDebug to show the files returned by model::index, I get something like this :
Normal dir is
a.txt
b.txt
c.txtfilePath in the loop returns this :
a.txt
b.txt
b.txtAnd c.txt does not get checked which is also odd.
-
Ok I found what was causing this awful bug.
When I check if the map contains the index, I actually check the raw index not the QPersistantModelIndex so this is not the same object and I think qmap does not check equality using == operator .
So I have to iterate over the map and check using QPersistantModelIndex "==" operators which can compare from a QModelIndex.