Results 1 to 6 of 6

Thread: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

  1. #1
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    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:
    Qt Code:
    1. - Local Drive
    2. + C:
    3. + D:
    4. + E:
    To copy to clipboard, switch view to plain text mode 
    Here is the code:
    Qt Code:
    1. pSystemPrimaryModel = new QFileSystemModel(this);
    2. pSystemPrimaryModel->setRootPath(QDir::currentPath());
    3. ui->TreeView->setModel(pSystemPrimaryModel);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    Please stop spamming. One thread for the problem is enough, you don't have to post in five different threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    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.

    Qt Code:
    1. QFileInfoList list = QFSFileEngine::drives();
    2. for(int i = 0; i < list.size(); i++)
    3. {
    4. qDebug() << list.at(i).absoluteDir();
    5. }
    6.  
    7. m_model = new QStandardItemModel(0,0);
    8.  
    9. QList<QStandardItem *> LocalItem;
    10. LocalItem.insert(0,new QStandardItem("Local Drives"));
    11. LocalItem.at(0)->setEditable(false);
    12. m_model->insertRow(0,LocalItem);
    13.  
    14.  
    15. for (int i = 0; i < list.size(); i++)
    16. {
    17. QString str = list.at(i).absolutePath();
    18. QStandardItem* Localchild = new QStandardItem(str);
    19. QStandardItem* LocalparentItem = m_model->item(0,0);
    20. Localchild->setEditable(false);
    21. LocalparentItem->appendRow(Localchild);
    22. }
    23.  
    24. ui->PrimTreeView->setModel(m_model);
    To copy to clipboard, switch view to plain text mode 
    Output:
    asdasd.JPG

    Quote Originally Posted by wysota View Post
    Please stop spamming. One thread for the problem is enough, you don't have to post in five different threads.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    Quote Originally Posted by owais_blore View Post
    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,
    _

  5. #5
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    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?
    Quote Originally Posted by anda_skoa View Post
    You are only asking for the drives, how do you expect the program to know that you want to recurse into them?

    Cheers,
    _

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt: How to add Custom ‘Local Drive’ row in QFileSystemModel to display Drives

    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,
    _

Similar Threads

  1. Replies: 6
    Last Post: 10th December 2012, 11:15
  2. Display System Drives in Specific Order in QTreeView
    By owais_blore in forum Qt Programming
    Replies: 6
    Last Post: 5th December 2012, 09:07
  3. Replies: 0
    Last Post: 20th November 2012, 11:05
  4. QFileSystemModel with network drives not connected
    By GothmoG in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 02:18
  5. Replies: 3
    Last Post: 17th August 2010, 18:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.