Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt TreeView获取考虑的总行数、展开和折叠文件夹数_C++_Qt_Qtreeview_Qabstractitemmodel - Fatal编程技术网

C++ Qt TreeView获取考虑的总行数、展开和折叠文件夹数

C++ Qt TreeView获取考虑的总行数、展开和折叠文件夹数,c++,qt,qtreeview,qabstractitemmodel,C++,Qt,Qtreeview,Qabstractitemmodel,我正在处理一个Qt应用程序,其中我希望检索目录/文件系统模型(如树)中可导航行的总数。这意味着,如果展开文件夹,则会添加其计数;如果折叠文件夹,则不会添加其计数。作为一个整体,我希望能够检索到每一行的数量是扩大和可用的。据我所知,在网上找不到这么容易的实现。两个尚未奏效的解决方案: int MainWindow::countRowsOfIndex_treeview( const QModelIndex & index ) { int count = 0; const QA

我正在处理一个Qt应用程序,其中我希望检索目录/文件系统模型(如树)中可导航行的总数。这意味着,如果展开文件夹,则会添加其计数;如果折叠文件夹,则不会添加其计数。作为一个整体,我希望能够检索到每一行的数量是扩大和可用的。据我所知,在网上找不到这么容易的实现。两个尚未奏效的解决方案:

int MainWindow::countRowsOfIndex_treeview( const QModelIndex & index )
{
    int count = 0;
    const QAbstractItemModel* model = index.model();
    int rowCount = model->rowCount(index);
    count += rowCount;
    for( int r = 0; r < rowCount; ++r )
        count += countRowsOfIndex_treeview( model->index(r,0,index) );
    return count;
}

但是,这不包括未扩展的child等。我希望我的问题是清楚的。感谢您的帮助。如果有人要求,我愿意提供更多的信息。谢谢。

如果每个索引都已展开或未展开,您可以查看您的视图。那么这只是一个遍历模型的问题

库巴订单信用证:

基于他出色的遍历功能:

void iterate(const QModelIndex & index, const QAbstractItemModel * model,
             const std::function<void(const QModelIndex&, int)> & fun,
             int depth = 0)
{
    if (index.isValid())
        fun(index, depth);
    if (!model->hasChildren(index)) return;
    auto rows = model->rowCount(index);
    auto cols = model->columnCount(index);
    for (int i = 0; i < rows; ++i)
        for (int j = 0; j < cols; ++j)
            iterate(model->index(i, j, index), model, fun, depth+1);
}
这样调用代码:

QTreeView view;
view.setModel(&model);
view.setWindowTitle(QObject::tr("Simple Tree Model"));

view.expandAll();
view.show();


qDebug() << "total expanded" << countExpandedNode(&view);
QTreeView;
view.setModel(&model);
view.setWindowTitle(QObject::tr(“简单树模型”);
view.expandAll();
view.show();

我认为你走的路不对。根据定义,节点是否展开由视图管理,模型对此一无所知。因此,最好查看视图API,例如这个API和boolqtreeview::isExpanded(constqmodelindex&index)const周围的其他API
int countExpandedNode(QTreeView * view) {
    int totalExpanded = 0;
    iterate(view->rootIndex(), view->model(), [&totalExpanded,view](const QModelIndex & idx, int depth){
        if (view->isExpanded(idx))
            totalExpanded++;
    });
    return totalExpanded;
}
QTreeView view;
view.setModel(&model);
view.setWindowTitle(QObject::tr("Simple Tree Model"));

view.expandAll();
view.show();


qDebug() << "total expanded" << countExpandedNode(&view);