PDA

View Full Version : Custom file system implementation in Qt



ayash
29th April 2016, 11:04
I am researching a way of implementing a simple file system UI. The requirement is that I have an in-memory tree (custom data structure), similar to a file system. But this is not the real file system of the computer. So, I want to represent this internal implementation to the user to react with that like adding new files, deleting files, editing files, adding folders, i.e. the basic operations.

I have no idea how to achieve this using Qt UIs. Is that a good idea to represent these files/folders using QLabel and setting pixmap for them? If I implement it like this, it is lot of work, like, once the folder is clicked, the frame should be cleared and the new set of labels should be loaded in the same frame.

What will be the easiest way?

d_stranz
29th April 2016, 18:30
Why don't you create a QAbstractItemModel to represent your internal tree structure, and then use a QTreeView to display it on screen? This is basically what QFileSystemModel does with the real file system.

QAbstractItemModel allows you to have an icon for each entry. When you implement the QAbstractItemModel::data() you just need to return the right thing for each of the Qt Roles. If you want to allow the user to change the model by interacting with the view, that's more work, and you'll have to implement the QAbstractItemModel::setData() method to do that.

There is a pretty good Qt example on implementing an editable tree model (http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html). I am not sure if it implements support for drag and drop to rearrange the tree, but you should be able to implement that once you have the basic model working.

ayash
4th May 2016, 08:05
thanks :) I'll try it

ayash
4th May 2016, 12:25
Can I know without having a tree model, is there a way to create a normal file-system-like interface like windows file-explorer or linux file-explorer with icon and double click on it?

anda_skoa
4th May 2016, 14:30
QListView in IconMode, with a list model that operates on one "directory"

Cheers,
_

ayash
4th May 2016, 16:37
Can you please send me an example or a code snippet for more understanding?

d_stranz
4th May 2016, 18:15
Can you please send me an example or a code snippet for more understanding?

If your "file system" is a hierarchy (that is, an item can have a parent as well as children), then a tree model is probably the best way to implement this, unless you only show one level at a time. That is, as anda_skoa suggested, your view consists of a simple list of the items at that level of the hierarchy, and you use some other way to move up or down.

In the old Windows FileManager program, the right side window displayed only a single level of the file system. To navigate to a lower level, you could double-click a directory entry; to go up a level, you double-clicked the ".." entry. The left side tree view was unnecessary for navigation.

You could derive a class from QStringListModel to represent the items at one level of your hierarchy. The only function you would have to reimplement is the data() method, and in that, the only thing you need to add is a case to handle the Qt::DecorationRole to return the icon for that entry. Everything else can be handled by the QStringListModel::data() implementation.

Google is your friend. There are many examples of how to use QListView, QStringListModel, and Qt::ItemDataRole.

anda_skoa
4th May 2016, 20:22
You could derive a class from QStringListModel to represent the items at one level of your hierarchy.
I would not start with a QStringListModel.
ayash mentioned icon, maybe even wants things like size, etc. so a custom list model or QStandardItemModel should provide a better start.

Cheers,
_

ayash
5th May 2016, 09:35
I would not start with a QStringListModel.
ayash mentioned icon, maybe even wants things like size, etc. so a custom list model or QStandardItemModel should provide a better start.

Yes, I need "set of icons" in my explorer. When I double click on the directory icon, it should go into that directory and if I double click on a file it should trigger a custom event a user can define it, likewise. Better if you can kindly provide me an example as I requested :)