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. 从网上搜索到的一些关于pcap源代码,入门级的

    /*pcap_1.c*/ #include <stdio.h>#include <stdlib.h>#include <pcap.h>  /* 如果没有pcap的系 ...

  2. 《从零开始学Swift》学习笔记(Day 7)——Swift 2.0中的print函数几种重载形式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift 2.0中的print函数有4种重载形式: l   print(_:).输出变量或常量到控制台,并且换行. l   print(_:_:).输出 ...

  3. 1060 最复杂的数(反素数玄学dfs)

    1060 最复杂的数 题目来源: Ural 1748 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中 ...

  4. Fraction

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  5. if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary

    if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary

  6. es 中 for in for of

    arr=[11,22,33,44,55,66,77,88]for (const i in arr){ console.log(i) if (i===4){ console.log(arr[i]) }} ...

  7. Power Systems 虚拟化简介

    本文向您详细地介绍了 Power System 虚拟化相关的技术和亮点,让您对这些最新的虚拟化技术有一个全面的了解.本文来自 IBM Systems Magazine for AIX 中文版. 自从引 ...

  8. PAT 1072. 开学寄语(20) JAVA

    下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其QQ,封其电脑,夺其手机,收其ipad,断其wifi,使其百无聊赖,然后,净面.理发.整衣,然后思过.读书.锻炼.明智.开悟.精进 ...

  9. 剑指offer 面试48题

    面试48题:题目:最长不含重复字符的子字符串 题:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长字符串的长度.假设字符串中只包含‘a’-‘z’的字符.例如,在字符串“arabcacfr ...

  10. 自制Javascript浮动广告

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb ...