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. jQuery EasyUI介绍

    官方定义:http://www.jeasyui.com/ what is JQuery EasyUI ? jQuery EasyUI framework helps you build your we ...

  2. 【BZOJ4825】[Hnoi2017]单旋 线段树+set

    [BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...

  3. 《从零开始学Swift》学习笔记(Day 53)——do-try-catch错误处理模式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil, ...

  4. javascript获取客户端默认打印机

    JS <script language="javascript"> function startRequest() { var oShell = new ActiveX ...

  5. EasyNVR浏览器无插件直播在Linux系统下将录像文件与EasyNVR可执行文件分离运行的方案

    问题背景 在工控机上运行EasyNVR,WEB访问出现设备在线,但是视频没有快照和无法正常直播: 问题原因分析 通过上工控机发现是由于磁盘空间被占满导致的软件运行收到影响. 解决问题分析 由于录像文件 ...

  6. vs编译程序不能实现,“未能完成操作 未指定的错误”的解决办法

    1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2005右边的“解决方案”处右键 重新加载,就 ...

  7. JavaScript事件onblur与onfocus区别

    一.onblur 1.1 说明 onblur属性在元素失去焦点时触发,onblur常用于表单验证代码(例如用户离开表单字段). 1.2 示例 <input type="text&quo ...

  8. android自定义控件(三)ProgressBar

     1.ProgressBar有两个进度,一个是android:progress,另一个是android:secondaryProgress.比如视频的缓存进度以及播放进度. 在这里缓存的进度就可以是a ...

  9. R语言(一)

    向量运算 R的强大功能之一就是把整个数据向量作为一个单一对象来处理.一个数据向量仅是数字的排列,一个向量可以通过如下方式构造 weight<-c(,,,) weight [] 结构c(--)用来 ...

  10. 【saltstack】saltstack执行结果和事件存储到mysql

    前言 项目中使用saltstack有一段时间了,之前都是在控制台操作,后来感觉越来越不方便,每次操作需要登陆服务器,还需要记一堆命令.最重要的是,公司进新人之后,新人由于不熟悉saltstack,容易 ...