(三)使用预定义模型QDirModel的例子
使用预定义模型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的例子的更多相关文章
- (二)使用预定义模型 QStringListModel例子
使用预定义模型 QStringListModel例子 源代码如下 Main.cpp #include <QApplication> #include "teamleadersdi ...
- scala中ClassOf、asInstenceOf、isInstanceOf三个预定义方法分析
classOf.isInstanceOf.asInstanceOf三个预定义方法分析 Scala的三个预定义(predefined)方法,我们经常用到:它们用来感觉很简单, 但是里面还是隐藏了一些细节 ...
- Java8学习笔记(二)--三个预定义函数接口
三个函数接口概述 JDK预定义了很多函数接口以避免用户重复定义.最典型的是Function: @FunctionalInterface public interface Function<T, ...
- TVM部署预定义模型
TVM部署预定义模型 本文通过深度学习框架量化的模型加载到TVM中.预量化的模型导入是在TVM中提供的量化支持之一. 本文演示如何加载和运行由PyTorch,MXNet和TFLite量化的模型.加载后 ...
- Shell 变量详解教程之位置变量与预定义变量。
Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一. 自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...
- 小鸟初学Shell编程(八)环境变量、预定义变量与位置变量
环境变量 环境变量:每个Shell打开都可以获得到的变量. 我们知道通过export的方式打开可以让子进程读取父进程的变量的值,那怎么样才能让每一个进程都能读取到变量的值呢? 在这呢,系统有一些默认的 ...
- Shell 变量详解教程之位置变量与预定义变量
Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一. 自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...
- C#预定义类型、引用类型
一.预定义的值类型 一个字节(1Byte)=8位(8Bit) BitArarry类可以管理位Bit. 1.整型 所有的整形变量都能用十进制或十六进制表示:long a=0x12AB 对一个整形值如未指 ...
- PHP魔术函数、魔术常量、预定义常量
一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...
随机推荐
- iptalbes -F
iptalbes -F -F, --flush [chain] Flush the selected chain (all the chains in the table if none is giv ...
- AP_付款方式汇总:标准付款、退款退货付款、撤销付款(概念)
2014-06-04 Created By BaoXinjian
- Adobe推出HTML5动画设计工具Edge
HTML5和Flash,是敌对?是共存? 虽然Flash如今依旧牢牢占领着网络动画的大半江山,但这样的状况终将会被改变. 那么,Edge的推出是否意味着Adobe将放弃和屈服于Flash与HTML5之 ...
- CTPN - 训练
源码地址:https://github.com/eragonruan/text-detection-ctpn 该地址提供了 CTPN 的 tf 版本的实现,代码文档写得很详细,issue 里面也帮助解 ...
- 山东大学硕士/博士研究生毕业论文--Latex模板
山东大学硕士/博士研究生毕业论文Latex模板 模板下载地址: https://github.com/Tsingke/SDU_thesis_template_for_postgraduate 封皮预 ...
- PHP采集库-Snoopy.class.php
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单. Snoopy的特点: 1.抓取网页的内容 fetch 2.抓取网页的文本内容 (去除HTML标签) fetchtext ...
- 为ubuntu添加多媒体以及flash等等常用包
因为多媒体mp3,mp4以及flash等等都是一些有版权的东东,ubuntu不能直接集成到安装盘内,所以,做了2个常用包,来存放它们. ubuntu-restricted-addons ubuntu- ...
- rabbitmq 常用的一些命令
rabbitmqctl set_user_tags admin administrator #给用户设置角色 rabbitmqctl set_permissions -p emove admin &q ...
- vue 阅读一【待完结】
初步方案:从第一个commit开始到到最近的commit,从代码的大纲开始到细节,利用思维导图. 注意: 源码阅读是一件比较枯燥的事情,要有别的东西一起做,源码只是偶尔看看,经常发呆的话,非常浪费时间 ...
- django:DateTimeField如何自动设置为当前时间并且能被修改 ——django日期时间字段的使用
创建django的model时,有DateTimeField.DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime().date().time()三中对象 ...