如果我理解正确你想要这样的东西:
我的代码有点粗糙,但您可以将其用作起点:
-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;
}
我希望这能帮到您。