4

while I'm working on something in Qt5 that closely resembles a file manager, I try to implement a very basic tree view, showing only the directory names without any other information. However, (it seems that) QTreeView doesn't let me decide which columns I want to show.

Here's what I have:

// ...
QString m_path = "C:/Users/mine";

dirModel = new QFileSystemModel(this);
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(m_path);

ui->treeView->setModel(dirModel);
// ...

Now my QTreeView shows more information with the name, like the size et al.; however, this is not the desired behavior.

Setting headerVisible to false removes the "headline" of my QTreeView which is OK, but how can I remove the other columns completely? I tried:

ui->treeView->hideColumn(1);

just to test if that works, but it did not change a thing.

r4ndom n00b
  • 89
  • 2
  • 8

2 Answers2

5
QTreeView* treeView = new QTreeView(centralWidget());
QFileSystemModel* fsModel = new QFileSystemModel(treeView);
fsModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
fsModel->setRootPath("/home/user");
treeView->setModel(fsModel);
// first column is the name
for (int i = 1; i < fsModel->columnCount(); ++i)
    treeView->hideColumn(i);

QHBoxLayout* hLayout = new QHBoxLayout(centralWidget());
hLayout->addWidget(treeView);

Another approach here (PyQt but the logic is still the same): PyQt: removing unnecessary columns

Community
  • 1
  • 1
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
2

There's nothing wrong with your approach. It works as below:

mainwindow header:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QFileSystemModel * dirModel;
};

mainwindow source:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString m_path = "E:";

    dirModel = new QFileSystemModel(this);
    dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    dirModel->setRootPath(m_path);

    ui->treeView->setModel(dirModel);

    ui->treeView->hideColumn(1);
}
user23573
  • 2,479
  • 1
  • 17
  • 36