Qt: Fails to display drives using QSystemStorageInfo
-
I am working on displaying all Local and removable drives of my system using QSystemStorageInfo in a QTreeView. I have written a code where all drives like C:, D: etc come under Local Drive section and SD_Card appears under Removable Drive section. Here is the code:
@#include <QtSystemInfo/QSystemStorageInfo>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);QStringList list = m_sysStorageInfo->logicalDrives(),listremovable,listlocal;
for(int i=0; i<list.count(); i++)
{
m_dtype = m_sysStorageInfo->typeForDrive(list[i]);
if ( m_dtype == QSystemStorageInfo::RemovableDrive)
{
listremovable<<list[i];
qDebug()<<listremovable;
}
else
{
listlocal<<list[i];
qDebug()<<listlocal;
}
}for (int i = 0; i < listlocal.count(); i++)
{
QString myString = listlocal.at(i);QStandardItem* Localchild = new QStandardItem(myString); QStandardItem* LocalparentItem = m_model->item(0,0); Localchild->setEditable(false); LocalparentItem->appendRow(Localchild);
}
for (int i = 0; i < listremovable.count(); i++)
{
QString myString = listremovable.at(i);QStandardItem* Removablechild = new QStandardItem(myString); Removablechild->setEditable(false); QStandardItem* RemovableparentItem = m_model->item(0,0); RemovableparentItem->appendRow(Removablechild);
}
ui->PrimTreeView->setModel(m_model);@
In header File:
@QSystemStorageInfo *m_sysStorageInfo;
QSystemStorageInfo::DriveType m_dtype;@When I run the application, it gives me C:, D: in my Local Section and SD_Card under Removable section. But when I click the C: drive or D: drive, it doesn't display the folders present inside it. Where am i wrong???
-
This is only part of code.
Your problem is most likely in UI.
Check what are you doing when you click on drive. -
When I click the drive, it doesn't display anything since there is no click event. Lets say i click C:, it should display folders present in the selected drive. Ideally what can be the best way to go ahead with???
[quote author="DRAX" date="1355145519"]This is only part of code.
Your problem is most likely in UI.
Check what are you doing when you click on drive.[/quote] -
Well, it seems that you are missing subelements (not loading) - you just load first level of directory structure (drives).
Take a look this video: "http://www.youtube.com/watch?v=M0PZDrDwdHM":http://www.youtube.com/watch?v=M0PZDrDwdHM
I think that this guy uses much simpler approach. -
Thanks DRAX. Sadly I dont have access to YouTube at Office. Any example or sample which can guide me?
[quote author="DRAX" date="1355146844"]Well, it seems that you are missing subelements (not loading) - you just load first level of directory structure (drives).
Take a look this video: "http://www.youtube.com/watch?v=M0PZDrDwdHM":http://www.youtube.com/watch?v=M0PZDrDwdHM
I think that this guy uses much simpler approach.[/quote] -
Well, from video what I saw he uses QDirModel, and then it sets to tree view.
Like this:
@
...
QDirModel *model = new QDirModel(this);
ui->PrimTreeView->setModel(model);
...
@
You might want to make model as private variable in header instead like this.Any time :)
-
Alright I just had a look at the video through some proxy site (hahaha). I noticed he used QModelIndex and hardcoded F: drive, he also set the expand and other properties..... When he built the app, the F: drive was expanded on startup itself.
What I ideally want to achieve is to click the drive and then expand rather than expanding by default on startup. I hope u got my point :)
[quote author="DRAX" date="1355147597"]Well, from video what I saw he uses QDirModel, and then it sets to tree view.
Like this:
@
...
QDirModel *model = new QDirModel(this);
ui->PrimTreeView->setModel(model);
...
@
You might want to make model as private variable in header instead like this.Any time :)[/quote]
-
Well, I looked at video again, and at 3:40 it doesn't open at startup.
When it clicks on drive then it opens.Sorry, but it seems that I don't understand what you mean.
-
Hi DRAX. Well thats because he is setting the model directly without any root node. Let me make it clear :)
Like you said in the above comment "Well, it seems that you are missing subelements (not loading)". By this you exactly got what I was trying to say. My code currently displays C:, D: etc but on clikcing each, it should display folders inside. Thats something which aint happening.
[quote author="DRAX" date="1355162051"]Well, I looked at video again, and at 3:40 it doesn't open at startup.
When it clicks on drive then it opens.Sorry, but it seems that I don't understand what you mean.[/quote]
-
Ok then, you need to implement it.
Give to each drive letter corresponding subelements.This should give you idea how to do it: "http://www.java2s.com/Code/Cpp/Qt/QTreeViewdemoandQStandardItem.htm":http://www.java2s.com/Code/Cpp/Qt/QTreeViewdemoandQStandardItem.htm
-
Hey I went through it yesterday itself. It gives me the same thing which my code displays. Damn I am really frustrated with this...... I have been goggling like anything from yesterday and didnt even find ONE post were people have come across this scenario :/
[quote author="DRAX" date="1355234958"]Ok then, you need to implement it.
Give to each drive letter corresponding subelements.This should give you idea how to do it: "http://www.java2s.com/Code/Cpp/Qt/QTreeViewdemoandQStandardItem.htm":http://www.java2s.com/Code/Cpp/Qt/QTreeViewdemoandQStandardItem.htm
[/quote]