How to manipulate multiple Folders/Dir in QT?

638 views Asked by At

I want to know how to manipulate directories until I get video files. Firstly the main Directory is "F:/TestingVideos/" Inside the test video there are files e.g:1. Cash Office ,2.Rosville Gate ,3. My Videos Each of this videos holds other folders for example Cash Office has a Directory of "F:/TestingVideos/Cash Office/" inside we have folders that have dates for example we have the following "F:/TestingVideos/Cash Office/20141201/" Inside the Date Folder I have videos that I want to play.

So far I have implemented a method:

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

}

my output is true for my QFileInfo info; for writeable and readable, also I do did a qDebug()<< info.absoluteFilePath() I got the following results: "F:/TestingVideos" "F:/" "F:/TestingVideos/Cash Office" "F:/TestingVideos/Rosville" "F:/TestingVideos/My Videos"

I want a way to manipulate the baseNames i.e. Cash Office, Rosville etc... Folders such that I can display their folders e.g. 20141201 in another combobox, since currently ui.monitoringcomboBox->show() can display the base names. I want to be able to manipulate the folders basically To a level where I where I can play a video using QUrl for example.

1

There are 1 answers

3
Sommerwild On BEST ANSWER

If i understood correctly you want something like this: enter image description here

My code is a bit rough but you can use it as starting point:

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

I hope this will help you.