-1

我想知道如何操作目录,直到我得到视频文件。首先主目录是"F:/TestingVideos/" 在测试视频里面有文件例如:1。现金办公室 ,2.Rosville Gate ,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 播放视频的级别。

4

1 回答 1

0

如果我理解正确你想要这样的东西: 在此处输入图像描述

我的代码有点粗糙,但您可以将其用作起点:

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

我希望这能帮到您。

于 2015-06-20T03:28:32.923 回答