使用预定义模型QDirModel的例子

Main.cpp

#include <QApplication>
#include "directoryviewer.h" int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DirectoryViewer directoryViewer;
directoryViewer.show();
return app.exec();
}

directoryviewer.h

#ifndef DIRECTORYVIEWER_H
#define DIRECTORYVIEWER_H #include <QDialog> class QDialogButtonBox;
class QDirModel;
class QTreeView; class DirectoryViewer : public QDialog
{
Q_OBJECT public:
DirectoryViewer(QWidget *parent = ); private slots:
void createDirectory();
void remove(); private:
QTreeView *treeView;
QDirModel *model;
QDialogButtonBox *buttonBox;
}; #endif

directoryviewer.cpp

#include <QtGui>

#include "directoryviewer.h"

DirectoryViewer::DirectoryViewer(QWidget *parent)
: QDialog(parent)
{
//创建一个目录模型
model = new QDirModel;
//可编辑
model->setReadOnly(false);
//初始排序属性 目录在前,然后文件
model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name); treeView = new QTreeView;
treeView->setModel(model);
treeView->header()->setStretchLastSection(true);
treeView->header()->setSortIndicator(, Qt::AscendingOrder);
treeView->header()->setSortIndicatorShown(true);
treeView->header()->setClickable(true); //当前目录的模型索引
QModelIndex index = model->index(QDir::currentPath());
//如果需要就打开它的父对象一直到根节点,并且调用scrollTo()滚动倒当前项,确保它是可见的
treeView->expand(index);
treeView->scrollTo(index);
//确保第一列足够宽,可以显示它所有的条目。
treeView->resizeColumnToContents(); buttonBox = new QDialogButtonBox(Qt::Horizontal);
QPushButton *mkdirButton = buttonBox->addButton(
tr("&Create Directory..."), QDialogButtonBox::ActionRole);
QPushButton *removeButton = buttonBox->addButton(tr("&Remove"),
QDialogButtonBox::ActionRole);
buttonBox->addButton(tr("&Quit"), QDialogButtonBox::AcceptRole); connect(mkdirButton, SIGNAL(clicked()),
this, SLOT(createDirectory()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(treeView);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout); setWindowTitle(tr("Directory Viewer"));
} void DirectoryViewer::createDirectory()
{
//获取当前目录 模型索引
QModelIndex index = treeView->currentIndex();
if (!index.isValid())
return;
//获取创建目录名
QString dirName = QInputDialog::getText(this,
tr("Create Directory"),
tr("Directory name"));
//创建子目录 mkdir(模型索引,目录名)
if (!dirName.isEmpty()) {
if (!model->mkdir(index, dirName).isValid())
QMessageBox::information(this, tr("Create Directory"),
tr("Failed to create the directory"));
}
} void DirectoryViewer::remove()
{
QModelIndex index = treeView->currentIndex();
if (!index.isValid())
return; //删除目录 rmdir(模型索引)
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 %1").arg(model->fileName(index)));
}

转自:http://qimo601.iteye.com/blog/1534325

(三)使用预定义模型QDirModel的例子的更多相关文章

  1. (二)使用预定义模型 QStringListModel例子

    使用预定义模型 QStringListModel例子 源代码如下 Main.cpp #include <QApplication> #include "teamleadersdi ...

  2. scala中ClassOf、asInstenceOf、isInstanceOf三个预定义方法分析

    classOf.isInstanceOf.asInstanceOf三个预定义方法分析 Scala的三个预定义(predefined)方法,我们经常用到:它们用来感觉很简单, 但是里面还是隐藏了一些细节 ...

  3. Java8学习笔记(二)--三个预定义函数接口

    三个函数接口概述 JDK预定义了很多函数接口以避免用户重复定义.最典型的是Function: @FunctionalInterface public interface Function<T, ...

  4. TVM部署预定义模型

    TVM部署预定义模型 本文通过深度学习框架量化的模型加载到TVM中.预量化的模型导入是在TVM中提供的量化支持之一. 本文演示如何加载和运行由PyTorch,MXNet和TFLite量化的模型.加载后 ...

  5. Shell 变量详解教程之位置变量与预定义变量。

    Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一.   自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...

  6. 小鸟初学Shell编程(八)环境变量、预定义变量与位置变量

    环境变量 环境变量:每个Shell打开都可以获得到的变量. 我们知道通过export的方式打开可以让子进程读取父进程的变量的值,那怎么样才能让每一个进程都能读取到变量的值呢? 在这呢,系统有一些默认的 ...

  7. Shell 变量详解教程之位置变量与预定义变量

    Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一.   自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...

  8. C#预定义类型、引用类型

    一.预定义的值类型 一个字节(1Byte)=8位(8Bit) BitArarry类可以管理位Bit. 1.整型 所有的整形变量都能用十进制或十六进制表示:long a=0x12AB 对一个整形值如未指 ...

  9. PHP魔术函数、魔术常量、预定义常量

    一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...

随机推荐

  1. OAF_OAF Framework学习笔记的基本概念(概念)

    2014-11-02 Created By BaoXinjian

  2. Android 自定义属性动画&Camera动画

      摘要: Android 自定义属性动画&Camera动画 1.相关知识点 对于Androi的帧动画,可以制作gif图片,有时为了能够动态的生成帧动画,就得需要使用代码构建了 Animati ...

  3. Android事件分发机制源代码分析

    小小感慨一下,做android有一段时间了,一直以来都是习惯整理笔记存到有道笔记上,没有写博客的习惯. 以后逐步分类整理出来,也算"复习"一遍了 - _ - . android的事 ...

  4. DB2中三个有关锁变量DB2_EVALUNCOMMITTED,DB2_SKIPDELETED和DB2_SKIPINSERTED的使用

    本文主要解释下DB2中三个有关锁变量DB2_EVALUNCOMMITTED,DB2_SKIPDELETED和DB2_SKIPINSERTED的使用 实验环境: DB2 v9.7.0.6 AIX 6.1 ...

  5. Scanner类nextInt方法的使用注意点

    一.先看一段正常的代码 1. 一段用Scanner捕获键盘输入的代码: Scanner sc = new Scanner(System.in); // 先读取键盘输入的字符串 System.out.p ...

  6. sqlserver把小数点后面多余的0去掉

    Sql中想把小数点后多余的0去掉,怎么办? select 5000/10000.0 --想变成0.5select 5500/10000.0 --想变成0.55select 5550/10000.0 - ...

  7. mysql创建数据库和删除数据库

    1.创建数据库 启动MySQL 服务之后,输入以下命令连接到MySQL 服务器: [mysql@db3 ~]$ mysql -uroot -p Enter password: Welcome to t ...

  8. javascript Set data structures

    集合(set)是一组无序的,但彼此之间又有一定相关性的数据集.每个成员在数组中只能出现一次. 在使用集合(set)之前最好先理解一下内容: 1.不包含任何成员的集合称为空集合. 2.如果两个集合的成员 ...

  9. CCDictionary(转)

    #ifndef __CCDICTIONARY_H__ #define __CCDICTIONARY_H__ //需要哈希表的支持 #include "support/data_support ...

  10. 每日英语:Burning Question / Does Reading In Dim Light Hurt Your Eyes?

    Mom always told us we'd go blind if we read in the dark. Does science back her up? Jim Sheedy, a doc ...