从组合框中选择目录,并在textedits中列出子目录

时间:2018-07-11 06:12:51

标签: qt qcombobox

我试图列出目录(从组合框中选择)中的所有子目录。现在,我可以在combobox中显示目录列表。我想选择其中一个目录,并在textedit中列出每个子目录(在此所选目录内)。 从组合框中选择目录时,我不知道如何列出子目录。这是我当前的代码。

func1 start. No deleter.
  Fred Constructor called.
   George Constructor called
func1 end.
   George Destructor called
  Fred Destructor called.
func2 start. Special deleter, no explicit destructor call.
  Fred Constructor called.
   George Constructor called
func2 end.
  cleanupGeorge() called
func3 start. Special deleter, explicit destructor call in deleter.
  Fred Constructor called.
   George Constructor called
func3 end.
  cleanupGeorge2() called
   George Destructor called
  Fred Destructor called.

我将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

我得到了答案。

void Combo_box::on_pushButton_clicked()
{
    QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
                                                       QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);

    ui->comboBox->setMinimumWidth(500);
    for(const QFileInfo & finfo: directory.entryInfoList()){
        ui->comboBox->addItem(finfo.absoluteFilePath());
    }

    connect(ui->comboBox, &QComboBox::currentTextChanged,[this](const QString &selectedDirectory) {
        QDirIterator it(selectedDirectory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);

        ui->textEdit->clear();
        while (it.hasNext()){
            ui->textEdit->append(it.next());
        }

    });
}