目录:
(一) Qt Model/View 的简单说明 、预定义模型
(二)使用预定义模型 QstringListModel例子
(三)使用预定义模型QDirModel的例子
(四)Qt实现自定义模型基于QAbstractTableModel
(五)Qt实现自定义模型基于QAbstractItemModel
(六) Qt实现自定义委托 QDelegate
使用预定义模型QDirModel的例子
Main.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
#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
#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)));
}
分享到:
相关推荐
讯飞星火模型代码例子讯飞星火模型代码例子讯飞星火模型代码例子讯飞星火模型代码例子讯飞星火模型代码例子讯飞星火模型代码例子
spark隐语义模型推荐,代码实现,Python来实现
三维模型库的建筑信息化模型(BIM)的英文全称是Building Information Modeling,是一个完备的信息模型,能够将工程项目在全生命周期中各个不同阶段的工程信息、过程和资源集成在一个模型中,方便的被工程各参与方...
linux设备模型代码例子。自己编写的,还不错的哦。
H5div盒子模型例子H5div盒子模型例子H5div盒子模型例子H5div盒子模型例子
基于隐语义模型的学生选课推荐算法.pdf
数学建模中随机模型的课件例子等,包括统计检验,聚类分析,主成分分析,还有一个实例
Android中使用JPCT+GLSurfaceView的方式加载三维模型文件,Android版本在5.0以上。
select 网络模型的使用例子
本资源是《C++中的单例模式及按需释放模型》系列博文的改进例子,文章地址为http://blog.csdn.net/gogogo/article/details/7041907,其中提到了如何在C++正确使用单例模式,并讲解了使用辅助类实现可以按需释放的...
基于Spark SQL+Spark MLlib+Spark Streaming+隐语义模型(LFM)+实际项目经验的个性化电影推荐系统源码+项目说明.zip 基于Spark SQL+Spark MLlib+Spark+隐语义模型(LFM)+实际项目经验的个性化电影推荐系统源码.zip ...
很简单的一个例子,还提供了两个OSG模型文件,能直接打开,可以看看模型是怎样移动的
windows socket网络编程之iocp完成端口模型的例子
车辆三自由度模型,有一定参考价值
.net一个简单的MVC模型例子,可以研究下。
详细的描述BA网络模型的构建,此程序基于matlab软件,为初学者提供一些指导
http://blog.csdn.net/infoworld/article/details/43935853 项目代码
使用opengl编写的测试模型视图矩阵的例子,可以通过在界面上调节参数非常形象地理解模型视图变换的结果是怎么样的。
通过wiki生成word2vec模型的例子,使用的中文 wiki资料