1

I am using QFileSystemModel with QTreeView to display the contents of root directory, now want to refresh the qtreeview whenever a drive is added or removed.

Tried with a refresh push button to achieve this with a slot, to delete the model and setting it to QTreeView again. but problem here is, its not expanding the column width to its contents after setting the model to the view on second time.

is there any best solution to achieve this. here is a code bit...

QTreeView fileExplorerTreeView = new QTreeView();
fileExplorerTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
fileExplorerTreeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

void loadFileExplorer()
{
    if(fileSystemModel)
    {
        delete fileSystemModel;
        fileSystemModel = 0;
     }

    fileSystemModel = new QFileSystemModel;
    fileSystemModel->setRootPath("");


    fileExplorerTreeView->setModel(fileSystemModel);

    QHeaderView* hHeader = fileExplorerTreeView->header();
    hHeader->hideSection(1);
    hHeader->hideSection(2);
    hHeader->hideSection(3);

    fileExplorerTreeView->resizeColumnToContents(0);
    fileExplorerTreeView->header()->setStretchLastSection(false);
}
Prady
  • 663
  • 3
  • 11
  • 28
  • Maybe i read the psot wrong, the problem is, not that the drives dont apper, but the columns width? – trompa Jul 23 '13 at 13:34
  • if model is updated with drives no need to reset the model to the view, so then i wont get the column width problem. both are related. – Prady Jul 24 '13 at 06:56
  • 1
    Have you tried with `void QHeaderView::setSectionResizeMode(int logicalIndex, ResizeMode mode)`? – trompa Jul 24 '13 at 08:31
  • Yes, thanks Trompa .. I added headerView->setResizeMode(QHeaderView::ResizeToContents); its working fine... this could be the answer, please add this in your answer I will accept it. – Prady Aug 02 '13 at 10:44

1 Answers1

0

The problem is that the model is not watching for fs changes, and not updating. And you want it to control at the higher level.

So, if you set your rootpath to my computer, it should work:

QString rPath = fileSystemModel->myComputer().toString(); 
fileSystemModel->setRootPath(rPath);

Here was a similar problem (changing filters didnt update model) solved this way.

Also, you should not have to resize the table with each change.

Just change the resize mode with:

void QHeaderView::setSectionResizeMode(int logicalIndex, ResizeMode mode)
Community
  • 1
  • 1
trompa
  • 1,967
  • 1
  • 18
  • 26