Reputation: 612
I am currently trying to make a QTreeView to display the contents of the folder on the computer. However, I experienced some weird issue where . and .. are displayed in the tree view which I do not want that to happen. How am I suppose to disable showing . and .. in the tree view?
Here is the code for the QTreeView.
model = new QDirModel(this);
model->setReadOnly(true);
model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
model->setFilter(QDir::Dirs);
ui->treeView->setModel(model);
// expand to D: Directory
QModelIndex index = model->index("D:/");
ui->treeView->expand(index);
ui->treeView->scrollTo(index);
ui->treeView->setCurrentIndex(index);
ui->treeView->resizeColumnToContents(0);
Upvotes: 2
Views: 3151
Reputation: 1078
You had a look at this? This is a standard Qt example given out with Qt creator, They are doing exact same what you want to do.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemModel model;
model.setFilter( QDir::Dirs | QDir::NoDotAndDotDot );
model.setRootPath("");
QTreeView tree;
tree.setModel(&model);
// Demonstrating look and feel features
tree.setAnimated(false);
tree.setIndentation(20);
tree.setSortingEnabled(true);
tree.setWindowTitle(QObject::tr("Dir View"));
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
tree.showMaximized();
#else
tree.resize(640, 480);
tree.show();
#endif
return app.exec();
}
Upvotes: 0
Reputation: 612
Finally figure out the answer:
model->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
Using the following will not work as the tree view can longer be expanded on each folder:
model->setFilter(QDir::Dirs);
model->setFilter(QDir::NoDotAndDotDot);
Upvotes: 2