QTreeView与QFileSystemModel:如何删除除“Name”之外的所有列?

时间:2022-07-25 15:05:42

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.

当我在Qt5中处理类似于文件管理器的内容时,我尝试实现一个非常基本的树视图,只显示目录名,不显示任何其他信息。然而,(看起来)QTreeView不允许我决定要显示哪些列。

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.

现在我的QTreeView显示了更多的信息,如size等。然而,这并不是我们所期望的行为。

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

将headerVisible设置为false将删除QTreeView的“标题”,这是可以的,但是如何完全删除其他列呢?我试着:

ui->treeView->hideColumn(1);

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

只是为了测试它是否有效,但它并没有改变什么。

2 个解决方案

#1


3  

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

这里的另一种方法(PyQt,但逻辑仍然相同):PyQt:删除不必要的列

#2


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

#1


3  

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

这里的另一种方法(PyQt,但逻辑仍然相同):PyQt:删除不必要的列

#2


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