Woody
Woody

Reputation: 612

QTreeView Display Directory

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);

Upvotes: 2

Views: 3151

Answers (2)

PRIME
PRIME

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

Woody
Woody

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

Related Questions