我正在尝试一个模型/视图编程的例子。
http://doc.qt.io/qt-5/model-view-programming.html
为了演示如何使用模型索引从模型中检索数据,我们在没有视图的情况下设置了一个QFileSystemModel,并在小部件中显示文件和目录的名称。虽然这并不显示使用模型的正常方式,但它演示了模型在处理模型索引时使用的约定。
我们以以下方式构造了一个文件系统模型:
QFileSystemModel *model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
int numRows = model->rowCount(parentIndex);在本例中,我们设置了默认的QFileSystemModel,使用该模型提供的索引()的特定实现获取父索引,并使用rowCount()函数计算模型中的行数。
这是我的密码:
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。
在"build-DemostrateQModelIndex-Desktop_Qt_5_5_1_GCC_64bit-Debug“文件夹中,有文件和文件夹。我希望行计数应该是内部项目的数量。
我还尝试初始化了QFileSystemModel;
QFileSystemModel* model = new QFileSystemModel;
model->setRootPath(QDir::rootPath());
QModelIndex parentIndex = model->index(QDir::currentPath());
qDebug() << "RowCount is " << model->rowCount(parentIndex);RowCount仍然是0。
更新1:应用Johannes Schaub的建议。我在代码中添加了一个QEventLoop。
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。
发布于 2015-11-05 12:40:14
QFileSystemModel使用延迟加载和延迟加载。您需要监视它的信号,这些信号将不断发出,直到加载完整个目录。
特别是,医生说
与QDirModel不同,QFileSystemModel使用一个单独的线程来填充自己,这样就不会在查询文件系统时导致主线程挂起。对rowCount()的调用将返回0,直到模型填充一个目录。
在您的示例中,您可能会运行一个本地QEventLoop,并将模型的各个信号(directoryLoaded)与事件循环的wait ()槽连接起来,以等待填充。我不确定在这个场景中是否可以使用canFetchMore和fetchMore来阻止等待人口(afaik的主要用途是当用户在无限列表中滚动时懒洋洋地加载,例如facebook针墙流)。至少值得一试。
@Kuba正确地指出,本地事件循环本质上不是必需的。如果您能够保留创建QFileSystemModel的上下文(例如,将其存储为指针成员),并以普通成员函数的形式对槽进行操作。
发布于 2019-02-26 10:44:35
使用的原则是:
directoryLoaded信号连接到插槽。当模型加载根路径时,将发送信号。model.canFetchMore和model.fetchMore与感兴趣文件夹的索引一起使用来完成的,然后立即返回(或者您可以尝试使用已经准备好的文件夹条目,但是这个替代方法需要管理模型准备就绪的进度)。model.fetchMore之后),测试canFetchMore将返回False,这意味着您感兴趣的文件夹已经完全加载,model.rowCount现在将返回正确的值。如果您对另一个文件夹感兴趣,请在新文件夹的索引中再次使用model.canFetchMore和model.fetchMore。您也可以将感兴趣文件夹的路径与传递给槽的参数进行比较,而不是使用model.canFetchMore。此字符串指示发送信号的路径。
您表示您使用的是C++,我没有这种语言中的代码,但是下面的Python代码可以很容易地翻译(self = this,缩进行相当于分隔块时的一对括号)
class MyWidget(QPlainTextEdit):
def __init__(self):
# Init superclass
super(MyWidget, self).__init__()
self.setReadOnly(True)
self.show()
# FS model, set root path
self.model = QFileSystemModel()
path = "C:/"
self.model.setRootPath(path)
# Perform next tasks after model root path is loaded
self.model.directoryLoaded.connect(self.on_loaded)
def on_loaded(self, loaded_path):
# Folder to list
folder_path = "C:/Users" # <--- we are interested in this folder,
# not the root folder
folder_index = self.model.index(folder_path)
# Check the folder we are interested in is completely loaded
if self.model.canFetchMore(folder_index):
self.model.fetchMore(folder_index)
return
# Folder is now loaded, list children
num_rows = self.model.rowCount(folder_index)
for row in range(num_rows):
# Child data
num_columns = self.model.columnCount(folder_index)
if num_columns > 0:
# Child name in first column
index = self.model.index(row, 0, folder_index)
text += index.data(Qt.DisplayRole)
# etc发布于 2022-10-16 16:19:06
int rowCount = ui->tableView->verticalHeader()->count();
https://stackoverflow.com/questions/33544645
复制相似问题