Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[solved] QSortFilterProxyModel used with QFileSystemModel crashes in derived QItemDelegate::sizeHint()
bmahf last edited by
I have been looking for an answer to this problem for some time now, but haven't found anything yet. I don't really see anyone else talking about this particular issue, but then again, it's not something most people would be wanting to do.
I have a QFileSystemModel that I use in a QTreeView to show the filesystem and click around into subfolders. This works correctly. I then added a class derived from QSortFilterProxyModel since I found that this was the correct way to do the filtering that I need, which is to only show USB (removable) drives. The derived proxy class works correctly, as its filterAcceptsRow() method figures out which of the drives to add returning true for any USB drive. But when the sizeHint() method for the delegate class derived from QItemDelegate is called, and the model is retrieved from the index, I call fileInfo(index) on that model and crash with a read access violation.
I commented out the body of the sizeHint() method, leaving only a return QSize(40,40); as return value, and no crash, and my window comes up with the correct list of USB drives shown, but when I double-click on either of the drives, it doesn't open up, even though there are subfolders on each.
I assumed that since I am casting the model pointer in sizeHint() to QFileSystemModel*, maybe I should be casting it to a pointer of my proxy model type, but that didn't work. Then I tried taking that cast pointer (my proxy model type) and called model->sourceModel() on it, which, if I was dealing with my own proxy model at this point, should have returned the actual QFileSystemModel as the source model. But calling fileInfo() on that pointer also crashes, but this time with ASSERT: "index.model() == q" in file dialogs\qfilesystemmodel.cpp, line 1320.
So I have two questions. 1) how to do correctly get at the fileInfo() method for my QFileSystemModel object, and 2) why is the QFileSystemModel not working to open subfolders?
I have pasted my code at http://pastebin.com/5SWFxGxV
Any help would be appreciated.
DBoosalis last edited by
Doe the code work without the proxy model ? If so I suspect the index is in reference to the proxy model.
How about adding some debug statements like
@qDebug() << index.isValid();
qDebug() << "text = " << index.data();
qDebug() << "Row = " << index.row() << "Column = " << index.column;
Are these values what you expect them to be ?
Lastly if the filter model is on then you probable have to use that.
Set a flag in the Delegate for proxy on/off.
Then you can do something like
QFileInfo info = proxy->fileInfo(index);
QFileInfo info = model->fileInfo(index);@
Hope this is of some help
bmahf last edited by
Thanks. That actually pointed me in the direction I needed to go. I replaced the original method code that I had with the following:
const QSortFilterProxyModel *proxy = reinterpret_cast<const QSortFilterProxyModel *>(index.model());
const QFileSystemModel *model = reinterpret_cast<const QFileSystemModel *>(proxy->sourceModel());
QFileInfo info = model->fileInfo(proxy->mapToSource(index));
So I got the proxy model pointer, then got the file system model pointer as its source model, which I'd already tried, but then I realized I hadn't mapped the index into the source model, so my call to model->fileInfo() now passes the file model index retrieved by mapping from proxy to source model. Now I'm fine with the crash. Thanks so very much for the pointer.