7.qfilesystemmodel rowcount 为什么为0? 一个简单的model类的例子
任务:
1.新建一个空的mainwindow项目
2.debug下编译得到一个文件夹,应用程序输出这个文件夹中的文件(不显示文件夹中的文件夹)
3.使用QFileSystemModel完成。

本例显示结果:
Makefile
Makefile.Debug
Makefile.Release
ui_mainwindow
(debug和release是文件夹,不在应用程序输出中)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> #include <QDebug>
#include <QDir>
#include <QFileSystemModel>
#include <QModelIndex>
#include <QFileInfo> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); private:
Ui::MainWindow *ui;
QFileSystemModel *model;
private slots:
void findDirectory(const QString &path);
}; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); model = new QFileSystemModel();
model->setRootPath(QDir::currentPath());
//这里直接调用rowCount函数返回0,
//QFileSystemModel是异步载入目录,当directoryLoaded信号发射之后,表示目录载入完成
//所以我们在槽中调用rowCount,返回正确的值。
connect(model, SIGNAL(directoryLoaded(QString)), this, SLOT(findDirectory(QString)));
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::findDirectory(const QString &path)
{
QModelIndex parentIndex = model->index(QDir::currentPath());
int row = model->rowCount(parentIndex); for(int i = ; i<row; i++) { QModelIndex index = model->index(i, , parentIndex);
QString text = index.data(Qt::DisplayRole).toString();
QString fullPath = QDir::currentPath().append("/").append(text); QFileInfo *fileInfo = new QFileInfo(fullPath); if(fileInfo->isFile())
qDebug() << text;
}
}
main.cpp
#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}
程序输出:

7.qfilesystemmodel rowcount 为什么为0? 一个简单的model类的例子的更多相关文章
- C++定义一个简单的Computer类
/*定义一个简单的Computer类 有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等, 有两个公有成员函数run.stop.cpu为CPU类的一个对象, ram为RAM类的一个对象, ...
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
- Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》
Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676
- CSS布局中一个简单的应用BFC的例子
什么是BFC BFC(Block Formatting Context),简单讲,它是提供了一个独立布局的环境,每个BFC都遵守同一套布局规则.例如,在同一个BFC内,盒子会一个挨着一个的排,相邻盒子 ...
- Oracle学习笔记:一个简单的行转列例子
一个简单的行列转换例子,原始数据. create table temp_cwh_student ( name ), subject ), score ) ) select * from temp_cw ...
- 一个简单的Spring测试的例子
在做测试的时候我们用到Junit Case,当我们的项目中使用了Sring的时候,我们应该怎么使用spring容器去管理我的测试用例呢?现在我们用一个简单的例子来展示这个过程. 1 首先我们新建一个普 ...
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下: C++ Code 1 2 template < class _Ty, cl ...
随机推荐
- hdoj-2141-Can you find it?(二分查找)
题目链接 /* Name:HDU-2141-Can you find it? Copyright: Author: Date: 2018/4/12 17:15:46 Description: 暴力,复 ...
- LeetCode Can Place Flowers
原题链接在这里:https://leetcode.com/problems/can-place-flowers/description/ 题目: Suppose you have a long flo ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- htaccess不起作用的解决方法,AllowOverride All打开后出现403错误时解决办法
在php程序的目录下有一个htaccess文件,这个文件起着对url重写的作用,但是不巧的,在我的应用程序里不起作用,baidu了一下,发现是 AllowOverride All, 这个选项没有打开 ...
- 学习动态性能表(16)--v$rowcache
学习动态性能表 第16篇--V$ROWCACHE 2007.6.12 本视图显示数据字典缓存(也叫rowcache)的各项统计.每一条记录包含不同类型的数据字典缓存数据统计,注意数据字典缓存有层次差 ...
- [Project Euler] 来做欧拉项目练习题吧: 题目013
问题描述: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072 ...
- SpringCloud微服务实战——第三章服务治理
Spring Cloud Eureka 服务治理 是微服务架构中最核心最基本的模块.用于实现各个微服务实例的自动化注册与发现. 服务注册: 在服务治理框架中,都会构建一个注册中心,每个服务单元向注册中 ...
- 使用PHP的GD2裁剪 + 缩放图片
/** * 裁剪 + 缩放图片 * @param array $params 包含x,y,width,height,path * @return string */ public function t ...
- linux 内存释放命令
我使用的是CentOS 6.5 ,由于卸载Solr 后发现内存占用挺多的,我想释放一下内存,就查阅了一些资料,分享给大家: 1.free -m 查看内存的使用情况,-m表示单位是兆 2.echo 1 ...
- MYSQL BENCHMARK()函数
MySQL有一个内置的BENCHMARK()函数,可以测试某些特定操作的执行速度. BENCHMARK(count,expr) BENCHMARK会重复计算expr表达式count次,通过这种方式就可 ...