使用预定义模型QDirModel+Treeview的例子

使用预定义模型QDirModel的例子


 
使用预定义模型QDirModel的例子
Main.cpp
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
 
Cpp代码  
#ifndef DIRECTORYVIEWER_H  
#define DIRECTORYVIEWER_H  
  
#include <QDialog>  
  
class QDialogButtonBox;  
class QDirModel;  
class QTreeView;  
  
class DirectoryViewer : public QDialog  
{  
    Q_OBJECT  
  
public:  
    DirectoryViewer(QWidget *parent = 0);  
  
private slots:  
    void createDirectory();  
    void remove();  
  
private:  
    QTreeView *treeView;  
    QDirModel *model;  
    QDialogButtonBox *buttonBox;  
};  
  
#endif  
 
 directoryviewer.cpp
 
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(0, 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(0);  
  
    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)));  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值