免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3205 | 回复: 0
打印 上一主题 下一主题

Qt学习之路(43): QDirModel [复制链接]

论坛徽章:
49
15-16赛季CBA联赛之福建
日期:2016-06-22 16:22:002015年亚洲杯之中国
日期:2015-01-23 16:25:12丑牛
日期:2015-01-20 09:39:23未羊
日期:2015-01-14 23:55:57巳蛇
日期:2015-01-06 18:21:36双鱼座
日期:2015-01-02 22:04:33午马
日期:2014-11-25 09:58:35辰龙
日期:2014-11-18 10:40:07寅虎
日期:2014-11-13 22:47:15申猴
日期:2014-10-22 15:29:50摩羯座
日期:2014-08-27 10:49:43辰龙
日期:2014-08-21 10:47:58
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-22 10:15 |只看该作者 |倒序浏览
  今天我们来看一个很有用的model:QDirModel。这个model允许我们在view中显示操作系统的目录结构。这次让我们先来看看运行结果:

  这个界面很熟悉吧?不过这可不是由QFileDialog打开的哦,这是我们自己实现的。而提供这种实现支持的,就是QDirModel和QTreeView。我们来看一下代码。
  mytreeview.h
#ifndef MYLISTVIEW_H
#define MYLISTVIEW_H
#include 
class MyTreeView : public QWidget
{
        Q_OBJECT
public:
        MyTreeView();
private:
        QDirModel *model;
        QTreeView *treeView;
private slots:
        void mkdir();
        void rm();
};
#endif // MYLISTVIEW_H
 
mytreeview.cpp
#include "mylistview.h"
MyTreeView::MyTreeView()
{
        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(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);
        QHBoxLayout *btnLayout = new QHBoxLayout;
        QPushButton *createBtn = new QPushButton(tr("Create Directory..."));
        QPushButton *delBtn = new QPushButton(tr("Remove"));
        btnLayout->addWidget(createBtn);
        btnLayout->addWidget(delBtn);
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->addWidget(treeView);
        mainLayout->addLayout(btnLayout);
        this->setLayout(mainLayout);
        connect(createBtn, SIGNAL(clicked()), this, SLOT(mkdir()));
        connect(delBtn, SIGNAL(clicked()), this, SLOT(rm()));
}
void MyTreeView::mkdir()
{
        QModelIndex index = treeView->currentIndex();
        if (!index.isValid()) {
                return;
        }
        QString dirName = QInputDialog::getText(this,
                                                                                        tr("Create Directory"),
                                                                                        tr("Directory name"));
        if (!dirName.isEmpty()) {
                if (!model->mkdir(index, dirName).isValid()) {
                        QMessageBox::information(this,
                                                                         tr("Create Directory"),
                                                                         tr("Failed to create the directory"));
                }
        }
}
void MyTreeView::rm()
{
        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 %1").arg(model->fileName(index)));
        }
}
          
                       
                               
编缉推荐阅读以下文章
                               

              
                         
  • Qt学习之路(47): 自定义Model之三
                        
                         
  • Qt学习之路(46): 自定义model之二
                        
                         
  • Qt学习之路(45): 自定义model之一
                        
                         
  • Qt学习之路(44): QSortFilterProxyModel
                        
                         
  • Qt学习之路(42): QStringListModel
                        
                         
  • Qt学习之路(41): QTableWidget
                        
                         
  • Qt学习之路(40): QTreeWidget
                        
                         
  • Qt学习之路(39): QListWidget
                        
                         
  • Qt学习之路(38): model-view架构
                        
                         
  • Qt学习之路(37): Qt容器类之关联存储容器
                        
                                   

                       
                       
         
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP