1.  QFileSystemModel 查看,添加 和 删除目录

2. 实现代码

dialog.h

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QtGui> class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(QWidget *parent = 0); private slots:
void createDirectory();
void remove(); private:
QFileSystemModel *model;
QTreeView *treeView; }; #endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("QFileSystemModel");
model = new QFileSystemModel;
model->setReadOnly(false);
model->setRootPath(QDir::currentPath()); treeView = new QTreeView;
treeView->setModel(model); treeView->header()->setStretchLastSection(true);
treeView->header()->setSortIndicator(0, Qt::AscendingOrder);
treeView->header()->setSortIndicatorShown(true);
treeView->header()->setClickable(true); QModelIndex index = model->index(QDir::currentPath());
treeView->expand(index);
treeView->scrollTo(index);
treeView->resizeColumnToContents(0); QPushButton *createButton = new QPushButton(tr("Create Dir"));
QPushButton *removeButton = new QPushButton(tr("Remove Dir"));
connect(createButton, SIGNAL(clicked()), this, SLOT(createDirectory()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(remove())); QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(createButton);
hLayout->addWidget(removeButton); QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(treeView);
vLayout->addLayout(hLayout); setLayout(vLayout);
} void Dialog::createDirectory()
{
QModelIndex index = treeView->currentIndex();
if( !index.isValid() )
return;
QString dirName = QInputDialog::getText(this, tr("create Dir"), tr("Dir name"));
if( !dirName.isEmpty() )
{
if( !model->mkdir(index, dirName).isValid() )
QMessageBox::information(this, tr("Create Dir"), tr("Failed to create Dir")); }
} void Dialog::remove()
{
QModelIndex index = treeView->currentIndex();
if( !index.isValid() )
return;
bool ok;
if( model->fileInfo(index).isDir() )
ok = model->rmdir(index);
else
ok = model->remove(index); if(!ok)
QMessageBox::information(this, tr("Remove"), tr("Failed to remove Dir").arg(model->fileName(index)));
}

main.cpp

#include "dialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.setSizeIncrement(400,300);
w.show(); return a.exec();
}

Qt QFileSystemModel QDirModel 示例代码, 使用方法的更多相关文章

  1. PyQt(Python+Qt)学习随笔:工具箱(QToolBox)编程使用的步骤及示例代码

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 使用toolBox开发应用时,通过Designer设计ui界面时,只能在Designer中设计too ...

  2. php示例代码之类似于C#中的String.Format方法

    php示例代码之类似于C#中的String.Format方法 原文来自于  http://stackoverflow.com/questions/1241177/c-string-format-equ ...

  3. My.Ioc 代码示例——属性和方法注入

    在 My.Ioc 中,我们可以指定让容器在构建好对象实例之后,自动为我们调用对象的公共方法或是为对象的公共属性赋值.在解析对象实例时,容器将根据我们在注册对象时指定的方法调用或属性赋值的先后顺序,调用 ...

  4. 重新 java 对象的 equals 和 hashCode 方法的建议和示例代码

    equals 方法 equals 方法需要满足的规范: 自反性: 对于任意非空引用 x, x.equals(x) 应该返回 true; 对称性: 对于任意引用, 当且仅当 x.equals(y) == ...

  5. C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法

    本文主要是实现操作系统级别的多进程间线程同步(进程同步)的示例代码及测试结果.代码经过测试,可供参考,也可直接使用. 承接上一篇博客的业务场景[C#使用读写锁三行代码简单解决多线程并发写入文件时线程同 ...

  6. Xamarin官方示例代码无法部署,提示已跳过部署解决方法

    最近利用Visual Studio 2017学习Android开发.主要是通过Xamarin官方的文档进行的.官方的入门指导提供了很多的示例代码.但是下载之后,调试运行的时候,总是无法部署到虚拟机上. ...

  7. Qt 5入门指南之Qt Quick编程示例

    编程示例 使用Qt创建应用程序是十分简单的.考虑到你的使用习惯,我们编写了两套教程来实现两个相似的应用程序,但是使用了 不同的方法.在开始之前,请确保你已经下载了QtSDK的商业版本或者开源版本,并且 ...

  8. Qt 事件使用示例 (一)

    Qt 事件使用示例,以一个常见的使用来说明:QLabel 当鼠标滑过时改变颜色. 事先说明要想实现这一功能有很多种方法,如Qss实现,本文使用Qt事件的方式来实现. 第一步,我们得实现一个从QLabe ...

  9. 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token

    1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...

随机推荐

  1. 【BZOJ3124】[Sdoi2013]直径 树形DP(不用结论)

    [BZOJ3124][Sdoi2013]直径 Description 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节 ...

  2. 【BZOJ2242】[SDOI2011]计算器 BSGS

    [BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...

  3. java学习笔记——数据类型及类型转换

    数据类型分为: 1.引用类型(字符型); 2.基本数据类型(数值型); 以下为基本数据类型介绍(括号内的数字表示该类型所占据的字节数) a.整型 byte(8)   short(16)   int(3 ...

  4. mysql 日期加减天数

    MySQL 为日期增加一个时间间隔:date_add() now()       //now函数为获取当前时间 select date_add(now(), interval 1 day); - 加1 ...

  5. android菜鸟学习笔记26----Android广播消息及BroadcastReceiver

    1.广播类型: Android中的广播有两种类型:标准广播和有序广播.其中,标准广播是完全异步发送的广播,发出之后,几乎所有的广播接收者都会在同一时刻收到这条广播消息,因而,这种类型的广播消息是不可拦 ...

  6. Introduction to Mathematical Thinking - Week 9 评论答案2

    根据 rubic 打分. 1. 我认为,如果说明 m, n 是自然数,所以最小值是 1 会更清楚.所以 Clarity 我给了 3 分.其他都是 4 分,所以一共是 23 分. 2.  我给出的分数 ...

  7. Markov Process

    w Markov Process -- from Wolfram MathWorld  http://mathworld.wolfram.com/MarkovProcess.html 谷歌背后的数学_ ...

  8. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  9. python函数补充

    一 作用域 作用域介绍 python中的作用域分4种情况: L:local,局部作用域,即函数中定义的变量: E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局      ...

  10. jQuery实现复选框全选、全不选、反选问题解析

    今天打算用jQuery实现一下复选框的全选.全不选和反选问题,刚开始用的是attr("checked",true/false)方法,发现只能在最开始实现一次全选,可以实现全不选,无 ...