简体   繁体   English

带有QFileSystemModel的QTreeView:如何删除除“名称”之外的所有列?

[英]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. 当我在Qt5中从事与文件管理器非常相似的工作时,我尝试实现一个非常基本的树形视图,仅显示目录名称,而不显示任何其他信息。 However, (it seems that) QTreeView doesn't let me decide which columns I want to show. 但是,(似乎) 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.; 现在,我的QTreeView用名称显示了更多信息,例如size等。 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? headerVisible设置为false可以删除我的QTreeView的“标题”,但是如何完全删除其他列? I tried: 我试过了:

ui->treeView->hideColumn(1);

just to test if that works, but it did not change a thing. 只是为了测试是否可行,但并没有改变任何事情。

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:删除不必要的列

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM