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.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With