c++ - Qt5 C++ : Remove item from QListView's model and how to count items

标签 c++ qt model qlistview qfilesystemmodel

1. 我正在尝试从 QListView 模型中删除几个列出的文件之一。我正在使用此代码,但它不起作用:

ui->listView->model()->removeRow(ui->viewFiles->currentIndex().row());

同样的事情是如果我使用:

model->removeRow(ui->viewFiles->currentIndex().row());

这段代码有什么问题,我如何从列表中删除一个项目?

2。 QFileSystemModel 只加载所选目录中的文件。我如何从 QListView 的模型中计算所有这些项目?

最佳答案

  1. removeRow(int row) 应该正常工作。确保 ui->viewFiles->currentIndex().row() 返回有效行(即介于 0 和 model->rowCount()-1 之间)。值得注意的是,您的 QModelIndex ui->viewFiles->currentIndex() 可能处于无效状态。您可以通过 ui->viewFiles->currentIndex().isValid() 进行检查。

  2. 如前所述,您可以通过 model->rowCount()

  3. 检查模型的行数

关于c++ - Qt5 C++ : Remove item from QListView's model and how to count items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17174239/

相关文章:

Qthread 在 Golang 中只发出一个信号

php - 如何访问新 View 文件中的模型数据?

javascript - 在 JavaScript 中构建模型

c++ - ComCtl32.dll 版本 6 与 Qt

c++ - QLocale toDate 总是在 "es"语言环境上返回无效的 QDate

python - Django,在数据库中存储函数

c++ - C/C++ : How does this inline if get parsed?

ostringstream 的 C++ 奇怪行为

c++ - 如何获得两个 std::map 的公共(public)键?

c++ - 根据鼠标和键盘交互检测计算机是否空闲