Qt QFileSystemModel QDirModel 示例代码, 使用方法

发布时间:2016-12-10 14:54:50 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Qt QFileSystemModel QDirModel 示例代码, 使用方法",主要涉及到Qt QFileSystemModel QDirModel 示例代码, 使用方法方面的内容,对于Qt QFileSystemModel QDirModel 示例代码, 使用方法感兴趣的同学可以参考一下。

1.  QFileSystemModel 查看,添加 和 删除目录   2. 实现代码 dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QtGui> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); private slots: void createDirectory(); void remove(); private: QFileSystemModel *model; QTreeView *treeView; }; #endif // DIALOG_H dialog.cpp #include "dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle("QFileSystemModel"); model = new QFileSystemModel; model->setReadOnly(false); model->setRootPath(QDir::currentPath()); 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); QPushButton *createButton = new QPushButton(tr("Create Dir")); QPushButton *removeButton = new QPushButton(tr("Remove Dir")); connect(createButton, SIGNAL(clicked()), this, SLOT(createDirectory())); connect(removeButton, SIGNAL(clicked()), this, SLOT(remove())); QHBoxLayout *hLayout = new QHBoxLayout; hLayout->addWidget(createButton); hLayout->addWidget(removeButton); QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(treeView); vLayout->addLayout(hLayout); setLayout(vLayout); } void Dialog::createDirectory() { QModelIndex index = treeView->currentIndex(); if( !index.isValid() ) return; QString dirName = QInputDialog::getText(this, tr("create Dir"), tr("Dir name")); if( !dirName.isEmpty() ) { if( !model->mkdir(index, dirName).isValid() ) QMessageBox::information(this, tr("Create Dir"), tr("Failed to create Dir")); } } void Dialog::remove() { 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 Dir").arg(model->fileName(index))); } main.cpp #include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.setSizeIncrement(400,300); w.show(); return a.exec(); }    

上一篇:httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
下一篇:C++/C的文件结构和程序版式

相关文章

相关评论