PDA

View Full Version : Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives



owais_blore
10th December 2012, 09:37
I am using QFileSystemModel to represent file structure through the QTreeView. Everything works fine and it displays C:, D: etc drives in TreeView, but I need to add an additional row at the beginning of the tree. For example for now is:

+ C:
+ D:
+ E:

After adding the row ‘Local Drives’ at the beginning, I wanna display it like:

- Local Drive
+ C:
+ D:
+ E:
Here is the code:

pSystemPrimaryModel = new QFileSystemModel(this);
pSystemPrimaryModel->setRootPath(QDir::currentPath());
ui->TreeView->setModel(pSystemPrimaryModel);

wysota
10th December 2012, 10:07
Please stop spamming. One thread for the problem is enough, you don't have to post in five different threads.

owais_blore
10th December 2012, 10:12
I am sorry. I tried using QFSFileEngine too but it just gives me a list of drives in 'Local Folder' but not the directories inside each drive. Can you please check and tel me whether this is a right way or not.


QFileInfoList 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);


for (int i = 0; i < list.size(); i++)
{
QString str = list.at(i).absolutePath();
QStandardItem* Localchild = new QStandardItem(str);
QStandardItem* LocalparentItem = m_model->item(0,0);
Localchild->setEditable(false);
LocalparentItem->appendRow(Localchild);
}

ui->PrimTreeView->setModel(m_model);
Output:
8492


Please stop spamming. One thread for the problem is enough, you don't have to post in five different threads.

anda_skoa
10th December 2012, 12:33
I am sorry. I tried using QFSFileEngine too but it just gives me a list of drives in 'Local Folder' but not the directories inside each drive.

You are only asking for the drives, how do you expect the program to know that you want to recurse into them?

Cheers,
_

owais_blore
11th December 2012, 13:02
Yes anda you are right. This scenario seems to be complicated. How can I make sure after clicking each drive, I should recurse into them?

You are only asking for the drives, how do you expect the program to know that you want to recurse into them?

Cheers,
_

anda_skoa
12th December 2012, 18:45
The tree view asks the model about the child count (rowCount) of each node. If the count is greater than 0 then the node will be visualized as expandable.
When the user expands that node, the view will ask for the children.

Since you are working with a model that stores all data in itself, you'll have to build the model that way, i.e. recurse through the data structure and create items accordingly.

Cheers,
_