2

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?

enter image description here

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);
Woody
  • 612
  • 9
  • 21

2 Answers2

2

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);
Woody
  • 612
  • 9
  • 21
0

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();
    }
PRIME
  • 1,058
  • 7
  • 21
  • Yeah, I had a look at this before, as I am using QtCreator for the interface, some part of code doesn't works well for me, but anyway, I got the problem sort out a couple of minutes ago – Woody Dec 16 '15 at 05:16
  • I am also using QtCreator and this code was supposed to work as is, since it was an example code supplied with Qt. I really wonder why it does not. – PRIME Dec 16 '15 at 05:20