如何在QT中操作多个文件夹/目录?

时间:2015-06-18 14:29:13

标签: c++ qt qmediaplayer qdir qfileinfo

我想知道在获取视频文件之前如何操作目录。 首先,主目录为"F:/TestingVideos/" 在测试视频中有一些文件,例如:1。现金办公室,2。罗斯维尔门,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-&gt; show()可以显示基本名称。我希望能够基本上操作文件夹到我可以使用QUrl播放视频的位置。

1 个答案:

答案 0 :(得分:0)

如果我理解正确你想要这样的东西: enter image description here

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

-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;
}

我希望这会对你有所帮助。