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 ...
随机推荐
- Unity自带寻路Navmesh
自带寻路Navmesh的三大组件: 1.Nav Mesh Agent:主要挂在寻路物体上 2.Off Mesh Link:实现区域转移功能(例如,有时不一定只是在地面上进行寻路,可能有些高高的平台,平 ...
- memcache内存分配问题
Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用.这里简单谈一下我对me ...
- 修改vmware中的FreeBSD配置
在运行虚拟机之前,将操作系统安装文件挂载到CD-ROM中,然后,启动虚拟机,并用root用户进入操作系统.做如下操作: 1:挂载光盘文件: #mount /cdrom 2:运行系统安装程序,就可以显示 ...
- Git 常用命令详解(三)
转自:http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html
- 使用swing构建一个界面(包含flow ,Border,Grid,card ,scroll布局)
package UI; import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Cursor;import ja ...
- Java处理乱码问题
中文乱码分为GET乱码和POST乱码 GET乱码在Tomcat中配置编码 <Connector port="8080" protocol="HTTP/1.1&quo ...
- CAS单点登录系统简介
一.cas简介 全名:Central Authentication Service特点: 1.开源的.多协议的 SSO 解决方案: Protocols : Custom Protocol . CAS ...
- [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!
//========没有这个规则(ETag是否移除) 20160422============// 200 OK (from cache) 与 304 Not Modified 为什么有的缓存是 20 ...
- angular +H5 上传图片 与预览图片
//index.html <form class="form-horizontal"> <div class="panel panel-default& ...
- grafana 4.0.2 + openfalcon query 1.4.3
转:http://blog.csdn.net/vbaspdelphi/article/details/53884282 grafana 4.0.2 加上官方app自带的openfalcon插件, 可以 ...