QT中如何操作多个Folders/Dir?

How to manipulate multiple Folders/Dir in QT?

我想知道在获得视频文件之前如何操作目录。 首先,主目录是 "F:/TestingVideos/" 在测试视频中有文件,例如:1。出纳处,2.Rosville 登机口,3。我的视频 每个视频都包含其他文件夹,例如 Cash Office 有一个 "F:/TestingVideos/Cash Office/" 目录,我们有包含日期​​的文件夹,例如我们有以下 "F:/TestingVideos/Cash Office/20141201/" 在日期文件夹中,我有我想播放的视频.

到目前为止我实现了一个方法:

void Dialog::on_loadedButton_clicked(){
  QString videoname = "F:/TestingVideos/";`
  ui->DirectoryLineEdit->setText(videoName);
  QDir dir(videoName);

QFileInfoList files = dir.entryInfoList();

QStringList MonTypeFolder = dir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::DirsFirst);
ui->MonitoringcomboBox->addItems( MonTypeFolder);
ui->MonitoringcomboBox->show();
foreach(QFileInfo file, files){
    if(file.isDir()){
        //qDebug() << "DIR: " << file.fileName();

        // qDebug() << "Directory path file:" << file.absoluteFilePath();
        QString filePathString = file.absoluteFilePath();
        QFileInfo info = QFileInfo(filePathString);

       qDebug() << "Folders" << " are writable: " << info.isWritable() << "are readable: " << info.isReadable();

    }
    if(file.isFile()){
          qDebug() << "FILE: " << file.fileName();
    }

}

我的输出对于我的 QFileInfo info; 可写和可读都是正确的,我也做了 qDebug()<< info.absoluteFilePath() 我得到了以下结果: "F:/TestingVideos" "F:/" "F:/TestingVideos/Cash Office" "F:/TestingVideos/Rosville" "F:/TestingVideos/My Videos"

我想要一种操作 baseNames 的方法,即 Cash Office、Rosville 等...文件夹,以便我可以显示它们的文件夹,例如20141201 在另一个组合框中,因为当前 ui.monitoringcomboBox->show() 可以显示基本名称。我希望能够基本上操作文件夹,达到可以使用 QUrl 播放视频的水平。

如果我没理解错你想要这样的东西:

我的代码有点粗糙,但你可以用它作为起点:

-MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QComboBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QComboBox *mainCB_;
    QComboBox *baseCB_;
    QComboBox *datesCB_;
    QComboBox *mediaCB_;

    QString path_;

    QStringList media_;

    void createComboBoxes();

private slots:
    void onBaseComboBoxActivated(QString folderText);

    void onDatesComboBoxActivated(QString folderText);

    void onMediaComboBoxActivated(QString folderText);
};

#endif // MAINWINDOW_H

-MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDir>
#include <QDebug>

#define CBWidth 140

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    media_()
{
    ui->setupUi(this);
    setFixedSize(840, 400);

    path_ = "/Users/its/Desktop/Testing Videos";  //insert your root folder path here
    media_ << "*.3gp" << "*.avi" << "*.m4v" << "*.mov" << "*.mp4" << "*.mpeg" << "*.mpg" << "*.3g2" << "*.mxf" << "*.swf" << "*.m2v" << "*.wmv" << "*.flv" << "*.mkv";

    QDir dir(path_);
    if(dir.exists())
    {
        createComboBoxes();
    }
    else
    {
        qDebug() << "Error: Main dir " << path_ << " doesn't exist.";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createComboBoxes()
{
    mainCB_ = new QComboBox(this);
    mainCB_->addItem(path_.section('/', -1));
    mainCB_->setFixedWidth(CBWidth);
    mainCB_->move(50, 50);

    baseCB_ = new QComboBox(this);
    baseCB_->setFixedWidth(CBWidth);
    baseCB_->move(250, 50);

    datesCB_ = new QComboBox(this);
    datesCB_->setFixedWidth(CBWidth);
    datesCB_->move(450, 50);

    mediaCB_ = new QComboBox(this);
    mediaCB_->setFixedWidth(CBWidth);
    mediaCB_->move(650, 50);

    QDir mainFolderDir(path_);
    QStringList mainFolderContent = mainFolderDir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::DirsFirst);
    baseCB_->addItems(mainFolderContent);
    connect(baseCB_, SIGNAL(activated(QString)), this, SLOT(onBaseComboBoxActivated(QString)));
    onBaseComboBoxActivated(baseCB_->itemText(0));
    connect(datesCB_, SIGNAL(activated(QString)), this, SLOT(onDatesComboBoxActivated(QString)));
    connect(mediaCB_, SIGNAL(activated(QString)), this, SLOT(onMediaComboBoxActivated(QString)));
}

void MainWindow::onBaseComboBoxActivated(QString folderText)
{
    QDir baseFolderDir(path_ + "/" + folderText);
    if(baseFolderDir.exists())
    {
        QStringList baseFolderContent = baseFolderDir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::DirsFirst);
        datesCB_->clear();
        datesCB_->addItems(baseFolderContent);
        onDatesComboBoxActivated(datesCB_->itemText(0));
    }
    else
    {
        qDebug() << "Error: Base dir " << path_ + "/" + folderText << " doesn't exist.";
    }
}

void MainWindow::onDatesComboBoxActivated(QString datesText)
{
    QDir datesFolderDir(path_ + "/" + baseCB_->currentText() + "/" + datesText);
    if(datesFolderDir.exists())
    {
        QStringList datesFolderContent = datesFolderDir.entryList(media_, QDir::Files, QDir::Name);
        mediaCB_->clear();
        mediaCB_->addItems(datesFolderContent);
        onMediaComboBoxActivated(mediaCB_->itemText(0));
    }
    else
    {
        qDebug() << "Error: Dates dir " << path_ + "/" + baseCB_->currentText() + "/" + datesText << " doesn't exist.";
    }
}

void MainWindow::onMediaComboBoxActivated(QString mediaText)
{
    qDebug() << "Media selected with URL:" << path_ + "/" + baseCB_->currentText() + "/" + datesCB_->currentText() + "/" + mediaText;
}

希望对您有所帮助。