Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTreeView with QFileSystemModel: How can I remove all columns except "Name"?

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.

like image 428
r4ndom n00b Avatar asked Oct 20 '25 11:10

r4ndom n00b


2 Answers

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

like image 143
ramtheconqueror Avatar answered Oct 23 '25 00:10

ramtheconqueror


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);
}
like image 42
user23573 Avatar answered Oct 23 '25 01:10

user23573