使用预定义模型QDirModel+Treeview的例子
2012-05-20 19:26
495 查看
使用预定义模型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)));
}
使用预定义模型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)));
}
相关文章推荐
- (三)使用预定义模型QDirModel的例子
- (二)使用预定义模型 QStringListModel例子
- 使用预定义模型 QStringListModel例子
- 使用预定义模型 QStringListModel+QStringlist例子
- (二)使用预定义模型 QStringListModel例子
- ThinkPHP--数据库连接及model模型定义使用
- QT目录模型QDirModel的使用
- Yii中Model(模型)的创建及使用方法
- Yii中Model(模型)的创建及使用方法
- 使用 Jackson 树模型(tree model) API 处理 JSON
- thinkphp框架函数switchModel(调用动态模型时使用)
- @classmethod, @staticmethod和@property这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:
- Xcode8.x使用CoreData模型出现类被非法重定义的解决办法
- Qt使用 QSqlTableModel 模型操作数据库
- phalcon Model模型的基本使用与常用操作(增删改查)
- 在模型和控制器之间使用ViewModel
- QSqlQueryModel使用例子
- 手动绑定TreeView,使用递归,绝对能用的例子
- 从一个工作流流转中人员设定的例子看WBISF流程引擎所定义的用户授权安全模型
- Thinkphp5关联模型多对多的定义与使用