⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directoryviewer.cpp

📁 Qt4的一些例子
💻 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());    treeView->expand(index);    treeView->scrollTo(index);    treeView->resizeColumnToContents(0);    mkdirButton = new QPushButton(tr("&Create Directory..."));    removeButton = new QPushButton(tr("&Remove"));    quitButton = new QPushButton(tr("&Quit"));    connect(mkdirButton, SIGNAL(clicked()),            this, SLOT(createDirectory()));    connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));    connect(quitButton, SIGNAL(clicked()), this, SLOT(accept()));    QHBoxLayout *buttonLayout = new QHBoxLayout;    buttonLayout->addWidget(mkdirButton);    buttonLayout->addWidget(removeButton);    buttonLayout->addStretch();    buttonLayout->addWidget(quitButton);    QVBoxLayout *mainLayout = new QVBoxLayout;    mainLayout->addWidget(treeView);    mainLayout->addLayout(buttonLayout);    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"));    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;    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)));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -