Reputation: 171
I am working on Qt app where I need to display Filesystem inside a Qtreeview
. Basically my idea is to customize the filesystem in such a way that Removable Drives come under one section and Local Drives come under another. I tried to achieve it using QFSFileEngine
but I have been successful to some extent. here is the code:
pSystemSecondaryModel = new QFileSystemModel(this);
pSystemSecondaryModel->setRootPath(QDir::currentPath());
list = QFSFileEngine::drives();
for(int i = 0; i < list.size(); i++)
{
qDebug() << list.at(i).absoluteDir();
}
m_model = new QStandardItemModel(0,0);
QList<QStandardItem *> LocalItem;
LocalItem.insert(0,new QStandardItem("Local Drives"));
LocalItem.at(0)->setEditable(false);
m_model->insertRow(0,LocalItem);
QList<QStandardItem *> RemovableItem;
RemovableItem.insert(0,new QStandardItem("Removable Drives"));
RemovableItem.at(0)->setEditable(false);
m_model->insertRow(1,RemovableItem);
for (int i = 0; i < list.count(); i++)
{
QString str = list.at(i).absolutePath();
Localchild = new QStandardItem(str);
QStandardItem* LocalparentItem = m_model->item(0,0);
Localchild->setEditable(false);
LocalparentItem->appendRow(Localchild);
}
QStandardItem* Removablechild = new QStandardItem();
QStandardItem* RemovableparentItem = m_model->item(1,0);
Removablechild->setEditable(false);
RemovableparentItem->appendRow(Removablechild);
ui->PrimTreeView->setModel(m_model);
When I run the app, it shows Local Drives and Removable Drives root node. Under Local Drive node, I find all C: D: E: etc as children whereas Removable Drive seems to be empty i.e. without any children. Here is the scenario which is the current output:
- Local Drives
C:/
D:/
E:/
- Removable Drives
But when I click on each drive, I am not able to see the subdirectories at all. Ideally when I click C:/ i should be able to see Windows
, Program Files
folder etc. Its totally empty. How can I achieve it? Please help
Upvotes: 1
Views: 1919
Reputation: 156
Forgive me if I misunderstood your question, but I think you are looking for something similar to this
Upvotes: 0
Reputation: 92637
As you probably know, you are not using your QFileSystemModel
at all, and simply populating a QStandardItemModel
with manual entries that you look up.
If your goal is to present custom groupings of file system listing, what you might try and do is use multiple QFileSystemModel
instances, with roots set to each of your drives. So you would continue to set up the top level drives as you are doing, but for each of those drives you might need a model set to that root. And then create a map between those items and their private models.
Then you would have to re-implement probably index, hasChildren, rowCount, columnCount, and maybe some other read-only methods, to check into the models of those drives. So if for instance the model needs to know if C:/
has children, your hasChildren()
would have to look up the mapping of that index to the hidden C:/
root model, and do a child count on that model.
Another way is to just subclass QStandardItemModel
(or QAbstractItemModel
) and do the file system listings yourself instead of using QFileSystemModel
. This would allow you to just set your fixed root items, being the categories, and drive letters, and then check the listings as they are expanded or collapsed. Again this will require subclassing a model which is not always a great beginners Qt experience.
Upvotes: 2