Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView: Root node isn't shown

QTreeView: Root node isn't shown

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Byron Lai
    wrote on last edited by
    #1

    I would like to use a QTreeView to display my home folder and its subdirectories
    Using the following Python code, the QTreeView excludes my home folder and only displays its subdirectories
    Is there any settings in QTreeView I can made to include the root node (home folder)?

    @model = QFileSystemModel()
    home_path = QDesktopServices.storageLocation(QDesktopServices.HomeLocation)
    root_index = model.setRootPath(home_path)

    tree = QTreeView()
    tree.setModel(model)
    tree.setRootIndex(root_index)
    tree.show()@

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Edico
      wrote on last edited by
      #2

      @
      model = new QFileSystemModel;

      QString home_files = QDesktopServices.storageLocation(QDesktopServices.HomeLocation);
      QModelIndex homeIndex = model->parent(model->index(home_files));
      QString home_path = model->filePath(homeIndex);
      QModelIndex root_index = model->setRootPath(home_path);
      
      treeView = new QTreeView;
      treeView->setModel(model);
      treeView->setRootIndex(root_index);
      
      QVBoxLayout *mainLayout = new QVBoxLayout;
      mainLayout->addWidget(treeView);
      
      setLayout(mainLayout);
      

      @

      You can use treeView->expand(), treeView->scrollTo() functions to expand the directory content and scroll to a specific item.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved