QFileSystemModel rowCount不能按预期工作 [英] QFileSystemModel rowCount does not work as expected

查看:145
本文介绍了QFileSystemModel rowCount不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模型/视图编程"中的示例.

I am try an example in Model/View Programming.

http://doc.qt.io/qt-5/model-view-programming.html

为演示如何使用模型索引从模型中检索数据,我们设置了没有视图的QFileSystemModel并在小部件中显示文件和目录的名称.尽管这并没有显示使用模型的正常方法,但是它演示了模型在处理模型索引时所使用的约定.

To demonstrate how data can be retrieved from a model, using model indexes, we set up a QFileSystemModel without a view and display the names of files and directories in a widget. Although this does not show a normal way of using a model, it demonstrates the conventions used by models when dealing with model indexes.

我们通过以下方式构建文件系统模型:

We construct a file system model in the following way:

QFileSystemModel *model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
int numRows = model->rowCount(parentIndex);

在这种情况下,我们设置默认的QFileSystemModel,使用该模型提供的index()的特定实现获取父索引,然后使用rowCount()函数对模型中的行数进行计数.

In this case, we set up a default QFileSystemModel, obtain a parent index using a specific implementation of index() provided by that model, and we count the number of rows in the model using the rowCount() function.

这是我的代码:

QFileSystemModel* model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
qDebug() << QDir::currentPath();
// "/media/Local Data/Files/Programming/C++/build-DemostrateQModelIndex-Desktop_Qt_5_5_1_GCC_64bit-Debug"
qDebug() << "RowCount is " << model->rowCount(parentIndex);

但是RowCount始终为0.

But RowCount is always 0.

在"build-DemostrateQModelIndex-Desktop_Qt_5_5_1_GCC_64bit-Debug"文件夹中,内部有文件和文件夹.我希望行数应该是里面的项目数.

In the "build-DemostrateQModelIndex-Desktop_Qt_5_5_1_GCC_64bit-Debug" folder, there is files and folder inside. I expect row count should be the number of items inside.

我也尝试初始化QFileSystemModel;

I also tried initialized the QFileSystemModel;

QFileSystemModel* model = new QFileSystemModel;
model->setRootPath(QDir::rootPath());
QModelIndex parentIndex = model->index(QDir::currentPath());
qDebug() << "RowCount is " << model->rowCount(parentIndex);

RowCount仍为0.

RowCount is still 0.

更新1: 应用约翰内斯·绍布(Johannes Schaub)的建议.我在代码中添加了QEventLoop.

Update 1: Applying the suggestion from Johannes Schaub. I add an QEventLoop to my code.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QFileSystemModel* model = new QFileSystemModel;
    model->setRootPath(QDir::rootPath());
    QModelIndex parentIndex = model->index(QDir::currentPath());
    qDebug() << QDir::currentPath();
    // "/media/Local Data/Files/Programming/C++/build-DemostrateQModelIndex-Desktop_Qt_5_5_1_GCC_64bit-Debug"
    qDebug() << "First RowCount Call is " << model->rowCount(parentIndex);

    QEventLoop loop;
    QObject::connect(model, &QFileSystemModel::directoryLoaded, &loop, &QEventLoop::quit);
    loop.exec();
    qDebug() << "RowCount Call after eventloop is  " << model->rowCount(parentIndex);

    return a.exec();
}

我仍然得到0的行计数.

I still get a row count of 0.

推荐答案

QFileSystemModel利用延迟加载和延迟加载.您需要注意其信号,该信号将不断发出,直到整个目录加载完毕.

QFileSystemModel utilizes lazy and deferred loading. You need to watch on its signals, which will be emitted constantly until the entire directory has been loaded.

特别是医生说

与QDirModel不同,QFileSystemModel使用一个单独的线程来填充自身,因此不会在查询文件系统时导致主线程挂起.调用rowCount()将返回0,直到模型填充目录为止.

Unlike QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.

在您的情况下,您可能会运行本地QEventLoop并将模型的相应信号(directoryLoaded)与事件循环的quit()插槽连接,以等待填充.我不确定canFetchMore和fetchMore是否可以用于这种情况下以及等待人口阻塞(afaik的主要用途是当用户在无限列表中向下滚动时(例如,facebook的pinwall流)延迟加载).至少值得尝试.

In your case, you could probably run a local QEventLoop and connect the respective signals (directoryLoaded) of the model with the quit() slot of the event loop to wait for the population. I am unsure whether canFetchMore and fetchMore can be used for this scenario aswell to block on waiting for the population (afaik its main use is lazy loading when the user scrolls down in an infinite list, like for example a facebook pinwall stream). It's worth an attempt, at least.

@Kuba正确地指出,本地事件循环本质上不是必需的.如果您可以负担得起创建QFileSystemModel的上下文(例如,通过将其存储为指针成员)并在插槽上作为普通成员函数来操作.

@Kuba notes correctly that a local event loop is not intrinsically required. If you can afford leaving the context in which you create the QFileSystemModel (by storing it as a pointer member for example), and acting on the slot as a normal member function.

这篇关于QFileSystemModel rowCount不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆