为啥在 QTreeView 中展开可能无法正常工作?

Posted

技术标签:

【中文标题】为啥在 QTreeView 中展开可能无法正常工作?【英文标题】:Why expanding in QTreeView may work incorrectly?为什么在 QTreeView 中展开可能无法正常工作? 【发布时间】:2020-04-05 11:02:59 【问题描述】:

我正在尝试创建一个小应用程序,它将查找、读取和导出一些 *.xml 文档。应用程序必须读取分层文件夹结构并在窗体上的 QTreeView 中将其可视化。为了更好地管理,我希望它在应用程序启动时扩展树视图的所有对象。我已经尝试了许多不同的解决方案,例如:

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,但我没有达到我想要的。

ma​​inwindow.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

ma​​inwindow.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 编程新手,知识还不全面,但我仍在搜索、阅读和尝试理解。

提前谢谢你,对英语不好表示抱歉。

app的所有代码:

ma​​in.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();

ma​​inwindow.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;
    QFileSystemModel fs_model;

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);
            // Recursively call the function for each child node.
            expandChildren(child, view);
        

        if (!view->isExpanded(index))
        
            view->expand(index);
        
    

    void expand_the_tree(const QModelIndex &root_index, QTreeView *view)
    

        qDebug() << fs_model.canFetchMore(root_index);

        while (fs_model.canFetchMore(root_index) == true)
        
            fs_model.fetchMore(root_index);
        

        qDebug().nospace() << "Model fetched on root layer.";

        QModelIndex child_1_index;
        QModelIndex child_2_index;
        QModelIndex child_3_index;

        view->expand(root_index);


        int root_index_rows = fs_model.rowCount(root_index);
        qDebug().nospace() << "Number of found profiles:" << root_index_rows;

        for (int i = 0; i < root_index_rows; ++i)
        
            child_1_index = fs_model.index(i,0,root_index);
            view->expand(child_1_index);
            expandChildren(child_1_index, view);
            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 = fs_model.index(j,0,child_1_index);
                //qDebug() << child_2_index;
                view->expand(child_2_index);
                int child_2_index_rows = fs_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 = fs_model.index(k,0,child_2_index);
                    view->expand(child_3_index);
                    int child_3_index_rows = fs_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;
                

            
        
    

;
#endif // MAINWINDOW_H

ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>

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() << "Preset's 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...";



    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->setModel(&fs_model);
    ui->treeView->setRootIndex(fs_index);
    ui->treeView->setCurrentIndex(fs_index);

    qDebug() << fs_model.canFetchMore(fs_index);

    for (int c = 1; c < fs_model.columnCount(); c++)
    
        ui->treeView->setColumnHidden(c, true);
    

    qDebug().nospace() << "View loaded. Expanding....";

    expand_the_tree(fs_index, ui->treeView);



MainWindow::~MainWindow()

    delete ui;



void MainWindow::on_treeView_clicked(const QModelIndex &index)

    qDebug() << index;


void MainWindow::on_pushButton_2_clicked()

    expandChildren(fs_index, ui->treeView);


void MainWindow::on_pushButton_clicked()

        expand_the_tree(fs_index, ui->treeView);

ma​​inwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>712</width>
    <height>635</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTreeView" name="treeView">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>60</y>
      <width>311</width>
      <height>531</height>
     </rect>
    </property>
   </widget>
   <widget class="QTableView" name="tableView">
    <property name="geometry">
     <rect>
      <x>370</x>
      <y>60</y>
      <width>321</width>
      <height>531</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>600</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Refresh</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>600</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Retrieve all</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

【参考方案1】:

这是整个代码吗?可以提供主要的吗?

无论如何我无法编译你的代码,第 29 行:

ui->treeView->setModel(fs_model);

调用不存在的函数treeView。

无论如何,如果你想做一个 UI,我建议你使用 QML,你可以看看 qt 中已经完成的示例。

树模型https://doc-snapshots.qt.io/qt5-5.11/qtquickcontrols-filesystembrowser-example.html

您可以在打开 Qt creator 时查看并运行它们。点击welcome,然后点击example,最后输入tree

一旦您有了基本代码,您就可以根据需要对其进行修改。

【讨论】:

QTreeView 确实有 setModel 方法,因为它继承自 QAbstractItemView:doc.qt.io/qt-5/qtreeview-members.html 不,是ui没有方法treeView。无论如何,如果您有其他代码要发布,请将其放在这里,以便我可以尝试:) mmm,你好像没用过Qt Designer(MOC),明显OP加了一个QTreeView,名字叫treeView,通过ui访问:ui-&gt;treeView 1.我读过树模型。 2. 是的,“treeView”是表单上的 QTreeView 对象,通过 UI 访问。 3.问题出在模型子索引的“rowCount”响应中。谷歌什么也没给... 我已经添加了整个代码。 .canFetchMode 返回 false,但当目录不为空且对象出现在 treeView 中时,.rowCount 继续返回零行...

以上是关于为啥在 QTreeView 中展开可能无法正常工作?的主要内容,如果未能解决你的问题,请参考以下文章

qt: pyqt: QTreeView 内部拖放几乎可以正常工作...被拖动的项目消失了

在 QTreeView 中找到 QModelIndex 可见行的更简单方法

为啥 clang 无法展开循环(即 gcc 展开)?

电脑上新装了金山词霸,为啥导致打开后无法正常工作呢

为啥 getter/setter 在 vue typescript 类组件中无法正常工作

主接线图设计过程中为啥要采用多级降压