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 ...
随机推荐
- CodeForces - 651D:Image Preview (双指针&)
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...
- CentOS X64上64位Oracle 11gR2 静默安装
CentOS 6.2 X64上64位Oracle 11gR2 静默安装 www.linuxidc.com/Linux/2012-03/56606p4.htm HP-UX静默安装oracle11g过程 ...
- PLSQL Developer 攻略
.Net程序员学用Oracle系列(18):PLSQL Developer 攻略 1.功能说明及使用技巧 1.1.对象浏览器 1.2.SQL 窗口 1.3.测试窗口 1.4.命令窗口 1.5.图表 ...
- Oracle 12C 新特性之 db默认字符集AL32UTF8、PDB支持不同字符集
一. db默认字符集AL32UTF8Specify the database character set when you create the database. Starting from Ora ...
- 洛谷P4721 【模板】分治 FFT(分治FFT)
传送门 多项式求逆的解法看这里 我们考虑用分治 假设现在已经求出了$[l,mid]$的答案,要计算他们对$[mid+1,r]$的答案的影响 那么对右边部分的点$f_x$的影响就是$f_x+=\sum_ ...
- 序章:为什么学习使用kotlin、及kotlin的一些碎碎念
为什么使用kotlin? 当然是因为项目目前的开发语言是kotlin啊! 一方面是想能够尽快适应项目,另一方面,kotlin这门语言独特的语法,确实很吸引我,也让我意识到java代码在某些程度上的繁琐 ...
- java文本文件读写
java的IO系统中读写文件使用的是Reader和Writer两个抽象类,Reader中的read()和close()方法是抽象方法,Writer中的write().flush()和close()方法 ...
- 聊聊“现在学习MFC有用吗?”
我用MFC做了4年多,后来转到WPF也做了快5年.对于二者,不敢说精通,但应该算入门.结合自己经历,如果不考虑项目需求,我认为新手学习WPF或许更好点.有3点: 1)大家都知道最近几年Motorola ...
- file“xxxxx”has modification times xxxxx s in the future..
这是因为一个项目从一个电脑拷贝的到另一个电脑上时,两个电脑的时钟不一致所致,修改一下项目所在目录的修改时间即可: find /your/dir -type f -exec touch {} + 也可以 ...
- Spring学习八
1: Tomcat容器四个等级? Container, Engine, Servlet容器, Context 真正管理Servlet的容器是Context容器:一个context对应一个web工程. ...