Qt QFileSystemModel QDirModel 示例代码, 使用方法
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 示例代码, 使用方法的更多相关文章
- PyQt(Python+Qt)学习随笔:工具箱(QToolBox)编程使用的步骤及示例代码
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 使用toolBox开发应用时,通过Designer设计ui界面时,只能在Designer中设计too ...
- php示例代码之类似于C#中的String.Format方法
php示例代码之类似于C#中的String.Format方法 原文来自于 http://stackoverflow.com/questions/1241177/c-string-format-equ ...
- My.Ioc 代码示例——属性和方法注入
在 My.Ioc 中,我们可以指定让容器在构建好对象实例之后,自动为我们调用对象的公共方法或是为对象的公共属性赋值.在解析对象实例时,容器将根据我们在注册对象时指定的方法调用或属性赋值的先后顺序,调用 ...
- 重新 java 对象的 equals 和 hashCode 方法的建议和示例代码
equals 方法 equals 方法需要满足的规范: 自反性: 对于任意非空引用 x, x.equals(x) 应该返回 true; 对称性: 对于任意引用, 当且仅当 x.equals(y) == ...
- C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法
本文主要是实现操作系统级别的多进程间线程同步(进程同步)的示例代码及测试结果.代码经过测试,可供参考,也可直接使用. 承接上一篇博客的业务场景[C#使用读写锁三行代码简单解决多线程并发写入文件时线程同 ...
- Xamarin官方示例代码无法部署,提示已跳过部署解决方法
最近利用Visual Studio 2017学习Android开发.主要是通过Xamarin官方的文档进行的.官方的入门指导提供了很多的示例代码.但是下载之后,调试运行的时候,总是无法部署到虚拟机上. ...
- Qt 5入门指南之Qt Quick编程示例
编程示例 使用Qt创建应用程序是十分简单的.考虑到你的使用习惯,我们编写了两套教程来实现两个相似的应用程序,但是使用了 不同的方法.在开始之前,请确保你已经下载了QtSDK的商业版本或者开源版本,并且 ...
- Qt 事件使用示例 (一)
Qt 事件使用示例,以一个常见的使用来说明:QLabel 当鼠标滑过时改变颜色. 事先说明要想实现这一功能有很多种方法,如Qss实现,本文使用Qt事件的方式来实现. 第一步,我们得实现一个从QLabe ...
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
随机推荐
- FastExcel遇到的问题
第一次使用FastExcel发现在创建excel文件的时候不成功,一直报这个问题: org.apache.poi.EmptyFileException: The supplied file was e ...
- 面向对象、接口编程的重要性 python 为什么引入接口interface
面向对象编程的实践 有个产品,其有10个子产品,现在要统计每日消费数据其中8个子产品的消费入账金额算法相同,2个不同; 拓展性差的糟糕的代码 def ConsumptionSum(product): ...
- Js slice()方法和splice()方法
1.slice(start,end) 从已有的数组中返回选定元素,参数start必填,end选填 <script> delArray(); function delArray(){ var ...
- Django报:builtin_function_or_method' object is not iterable
def detail(request,hero_id): hero=models.HeroInfo.objects.get(id=hero_id) return render_to_response( ...
- word中插入的代码库设置局部背景色
https://zhidao.baidu.com/question/1494951482361210539.html
- Python3.6全栈开发实例[023]
23.税务部门征收所得税. 规定如下: (1)收入在2000以下的. 免征. (2)收入在2000-4000的, 超过2000部分要征收3%的税. (3)收入在4000-6000的, 超过4000部分 ...
- linux安装jdk_1.8
转载自http://blog.csdn.net/ldl22847/article/details/7605650 1.下载jdk的rpm安装包,这里以jdk-8u141-linux-x64.rpm为例 ...
- Linux学习笔记(1)linux的开关机及重启
linux的启动流程 一.启动 (1)电源 开关 (2)选择启动方式:FLOPPY/BIOS/CDROM(软盘/bios启动/光盘) 基于MBR引导方式 [1]MBR:最多只能划分4个主分区,逻辑 ...
- kubernetes 搭建教程
http://blog.csdn.net/u011563903/article/details/71037093
- LeetCode::Sort List 具体分析
Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...