我正在尝试创建一个小型应用程序,它将查找,读取和导出一些* .xml文档。应用程序必须阅读分层文件夹结构,并在窗体上的QTreeView中对其进行可视化。为了更好地管理,我希望它在应用程序启动时扩展treeview的所有对象。我已经尝试过许多不同的解决方案,例如:
void expandChildren(const QModelIndex &index, QTreeView *view)
{
if (!index.isValid())
{
return;
}
int childCount = index.model()->rowCount(index);
for (int i = 0; i < childCount; i++)
{
const QModelIndex &child = index.child(i, 0);
expandChildren(child, view);
}
if (!view->isExpanded(index))
{
view->expand(index);
}
}
[来自一些论坛和嵌入式解决方案,例如QTreeView :: expandRecursively和QTreeView :: expandAll,但我还没有实现我想要的。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include <QDebug>
#include <QDirModel>
#include <QTreeView>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
QModelIndex fs_index;
QModelIndex child_1_index;
QModelIndex child_2_index;
QModelIndex child_3_index;
QFileSystemModel *fs_model = new QFileSystemModel;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_treeView_clicked(const QModelIndex &index);
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
void expandChildren(const QModelIndex &index, QTreeView *view)
{
if (!index.isValid())
{
return;
}
int childCount = index.model()->rowCount(index);
for (int i = 0; i < childCount; i++)
{
const QModelIndex &child = index.child(i, 0);
expandChildren(child, view);
}
if (!view->isExpanded(index))
{
view->expand(index);
}
}
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString str_root_path;
QDir dir;
qDebug() << "Current user's home folder is: " <<QDir::home().path();
str_root_path = QDir::home().path() + "/AppData/Local/app/settings";
qDebug() << "Settings storing folder id: " << str_root_path;
dir.setPath(str_root_path);
if (!dir.exists())
{
qDebug() << "Settings folder doesn't exist.";
return;
}
if (!dir.isReadable())
{
qDebug() << "Folder found. Read access denied. Check access rights.";
return;
}
qDebug() << "Folder found. Read access granted. Reading...";
ui->treeView->setModel(fs_model);
fs_index = fs_model->index(str_root_path);
qDebug() << fs_model->fileName(fs_index);
fs_model->setRootPath(str_root_path);
QStringList filter;
filter << "*.xml";
fs_model->setNameFilters(filter);
fs_model->setFilter( QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
ui->treeView->setRootIndex(fs_index);
ui->treeView->setCurrentIndex(fs_index);
for (int i = 1; i < fs_model->columnCount(); i++)
{
ui->treeView->setColumnHidden(i, true);
}
ui->treeView->show();
qDebug().nospace() << "View loaded. Expanding....";
ui->treeView->setExpanded(fs_index, true);
int fs_index_rows = fs_model->rowCount(fs_index);
qDebug().nospace() << "Number of found profiles:" << fs_index_rows;
for (int i = 0; i < fs_index_rows; ++i)
{
child_1_index = fs_model->index(i,0,fs_index);
ui->treeView->setExpanded(child_1_index, true);
int child_1_index_rows = fs_model->rowCount(child_1_index);
qDebug().nospace() << "Step #" << i+1 << " Object name: " << fs_model->fileName(child_1_index) << ". Num of children: " << child_1_index_rows;
for (int j = 0; j < child_1_index_rows; ++j)
{
child_2_index = ui->treeView->model()->index(j,0,child_1_index);
//qDebug() << child_2_index;
ui->treeView->setExpanded(child_2_index, true);
int child_2_index_rows = ui->treeView->model()->rowCount(child_2_index);
qDebug().nospace() << "Step #" << i+1 << "/" << j+1 << " Object name: " << fs_model->fileName(child_1_index) << "/" << fs_model->fileName(child_2_index) << ". Num of children: " << child_2_index_rows;
for (int k = 0; k < child_2_index_rows; ++k)
{
child_3_index = ui->treeView->model()->index(k,0,child_2_index);
ui->treeView->setExpanded(child_3_index, true);
int child_3_index_rows = ui->treeView->model()->rowCount(child_3_index);
qDebug().nospace() << "Step #" << i+1 << "/" << j+1 << "/" << k+1 << " Object name: " << fs_model->fileName(child_1_index) << "/" << fs_model->fileName(child_2_index) << "/" << fs_model->fileName(child_3_index) << ". Num of children: " << child_3_index_rows;
}
}
}
}
例如,如果将代码粘贴到与“ pushbutton_Clicked”信号相连的插槽中,则每次单击都会将树视图扩展到一个更高的深度级别(如果我将QTreeView :: expandRecursively或QTreeView :: expandAll连接起来,则会出现相同的动作到“ pushbutton_Clicked”)。我试图调试应用程序的每个步骤,但我发现每个新的索引对象都无法获得文件系统模型的父级索引。
请帮助我理解,错误在哪里以及如何解决。
我对Qt编程尚不熟悉,我的知识还不完整,但我仍在搜索,阅读和尝试理解。
先谢谢您,英语不好。
这是整个代码吗?您还可以提供主要服务吗?
无论如何,我无法编译您的代码,第29行:
ui->treeView->setModel(fs_model);
调用函数treeView,该函数不存在。
无论如何,我建议您使用QML,并且可以看一下qt中已经完成的示例。
树模型https://doc-snapshots.qt.io/qt5-5.11/qtquickcontrols-filesystembrowser-example.html
打开Qt Creator时,您可以查看并运行它们。您单击welcome,然后单击example,最后键入tree
一旦有了基本代码,就可以根据需要对其进行修改。