1 Star 11 Fork 6

胡歌-此生不换/Qt-Project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Qt MVC 结构之 Model 模型介绍

MVC 简介

MVC 就是 Model-View-Control 模式的简称,包括模型层(Model), 视图层(View), 控制层(Controller)。

Model 主要负责管理数据,View 主要用来显示数据,Controller 主要用来操作数据,控制 View 联动。

Qt 也采用了这个模式,模型层用 Model,视图层用 View,控制层改名叫了代理 Delegate

QFileSystemModel

我们可以举个简单的例子,用 QFileSystemModel 来实现文件夹内容的展示,QFileSystemModelQt 给我们提供的处理本地文件系统的文件和目录。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // MainWindow w;
    // w.show();

    QSplitter* splitter = new QSplitter;
    QFileSystemModel* model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());

    // 下面采用了两种方式,对同一个文件夹中的内容进行了展示
    QTreeView* tree = new QTreeView(splitter);
    tree->setModel(model);
    tree->setRootIndex(model->index(QDir::currentPath()));

    QListView* list = new QListView(splitter);
    list->setModel(model);
    list->setRootIndex(model->index(QDir::currentPath()));

    splitter->setWindowTitle("two views onto the same file system model");
    splitter->resize(1000, 800);
    splitter->show();

    // 运行起来,可以看到,使用 tree 的方式是更好,更美观的,linux 也是采用的 tree 的方式

    return a.exec();
}

model 设置根目录,目录为当前文件夹。

创建树型视图,视图加载 model,并且设置视图的根索引为 model 的当前目录的索引。

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ax020913/qt-project.git
git@gitee.com:ax020913/qt-project.git
ax020913
qt-project
Qt-Project
master

搜索帮助