1. 文件目录浏览器

直接在main.cpp文件中添加下列代码

#include "mainwindow.h"

#include <QApplication>
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QItemSelectionModel> #include <QDirModel>
#include <QTreeView>
#include <QListView>
#include <QTableView>
#include <QSplitter> // MV model view
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
//首先创建一个文件模型
QDirModel model;
//三种显示模式
QTreeView tree;
QListView list;
QTableView table;
// 设置view对象的model
tree.setModel(&model);
list.setModel(&model);
table.setModel(&model);
tree.setSelectionMode(QAbstractItemView::SingleSelection); //单选
// tree.setSelectionMode(QAbstractItemView::MultiSelection); //多选
list.setSelectionMode(QAbstractItemView::MultiSelection); //多选
// table.setSelectionMode(tree.selectionModel()); //多选
table.setSelectionMode(QAbstractItemView::MultiSelection); //多选
QObject::connect(&tree, SIGNAL(doubleClicked(QModelIndex)), &list, SLOT(setRootIndex(QModelIndex)));
QObject::connect(&tree, SIGNAL(doubleClicked(QModelIndex)), &table, SLOT(setRootIndex(QModelIndex)));
QSplitter* splitter = new QSplitter;
// 添加布局
splitter->addWidget(&tree);
splitter->addWidget(&list);
splitter->addWidget(&table);
splitter->setWindowTitle(QObject::tr("Model/View"));
splitter->show();
return a.exec();
}

为了实现双击QTreeView对象中的某个目录时,QListView对象和QTableView对象中显示此选定目录下的所有文件和目录,需要连接QTreeView对象的doubleClicked()信号与QListView对象和QTableView对象的setRootIndex()槽函数。

    QObject::connect(&tree,   SIGNAL(doubleClicked(QModelIndex)), &list, SLOT(setRootIndex(QModelIndex)));
QObject::connect(&tree, SIGNAL(doubleClicked(QModelIndex)), &table, SLOT(setRootIndex(QModelIndex)));

其中 setRootIndex的介绍如下

QModelIndex的介绍如下



QModelIndex类用于定位数据模型中的数据。

doubleClicked信号如下,这个信号不是在tree里面的而是QAbstractItemView里面的

QAbstractItemView::doubleClicked(const QModelIndex &index)

This signal is emitted when a mouse button is double-clicked. The item the mouse was double-clicked on is specified by index. The signal is only emitted when the index is valid.

这个信号在双击鼠标按钮时发出。鼠标双击的项目由index指定。该信号只在索引有效时发出。

2.运行效果

Qt Model/view 小实例 文件目录浏览器的更多相关文章

  1. Qt Model/View(官方翻译,图文并茂)

    http://doc.trolltech.com/main-snapshot/model-view-programming.html 介绍 Qt 4推出了一组新的item view类,它们使用mode ...

  2. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  3. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  4. (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例

    Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...

  5. (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介

    Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...

  6. (一) Qt Model/View 的简单说明

    (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型基于QA ...

  7. qt model/view/delegate

    Qt Model/View理解(一)---构造model https://blog.csdn.net/weixin_42303052/article/details/89233887 Qt Model ...

  8. Qt Model/View 的简单说明

    目录: (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型 ...

  9. Qt Model/View学习(二)

    Model和View的搭配使用 DEMO pro文件 #------------------------------------------------- # # Project created by ...

随机推荐

  1. redis常用命令练习

    redis-server redis-cli select 0-15 redis key: string\hash\list\set\sortedset 1.增删改查... keys * 所有key ...

  2. 我的N年软件测试感悟

    1.前言 大家好!我是Meng前段时间,很荣幸被一合作伙伴邀请发表一篇文章,主题为"这些年,我所从事软件测试的一些感悟",正好趁着这个机会,我也好好总结一下. 2.测试培训 对于软 ...

  3. 03:进程Queue --- 生产者消费者模型

    1 进程Queue介绍 1 进程间数据隔离,两个进程进行通信,借助于Queue​2 进程间通信:IPC -借助于Queue实现进程间通信    -借助于文件        -借助于数据库    -借助 ...

  4. SpringBoot实现通用的接口参数校验

    本文介绍基于Spring Boot和JDK8编写一个AOP,结合自定义注解实现通用的接口参数校验. 缘由 目前参数校验常用的方法是在实体类上添加注解,但对于不同的方法,所应用的校验规则也是不一样的,例 ...

  5. StackOverflow经典问题:代码中如何去掉烦人的“!=null"判空语句

    问题 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗!如何避 ...

  6. 【题解】poj 3162 Walking Race 树形dp

    题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...

  7. Pytorch项目基本结构

    梳理一下Pytorch项目的基本结构(其实TF的也差不多是这样,这种思路可以迁移到别的深度学习框架中) 结构树 -------checkpoints #存放训练完成的模型文件 ​ ----xxx.pk ...

  8. 基于ABP落地领域驱动设计-04.领域服务和应用服务的最佳实践和原则

    目录 系列文章 领域服务 应用服务 学习帮助 系列文章 基于ABP落地领域驱动设计-00.目录和前言 基于ABP落地领域驱动设计-01.全景图 基于ABP落地领域驱动设计-02.聚合和聚合根的最佳实践 ...

  9. 在js中使用moment将秒转换为多少天多少小时多少分多少秒

    let x = 2703750;//单位是秒 var d = moment.duration(x, 'seconds'); console.log(Math.floor(d.asDays()) + ' ...

  10. UnityPlayerActivity.java使用或覆盖了已过时的 API。

    Root\Temp\gradleOut\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java使用或覆盖了已过时的 ...