Qt: Display Filesystem in A Customized Way inside QTreeView
-
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 because I am not passing the Filesystem instance inside SetModel. If I do that, it displays all the drives which is something I don want. Ideally with this approach above, 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
-
I think the only way is to just subclass QStandardItemModel (or QAbstractItemModel) and do the file system listings instead of using QFileSystemModel. But it looks complicated. Can anyone help me achieve this?