如何从 lambda 捕获父目录

How to capture parent directory from lambda

在我的代码中,我列出了子目录名称(仅子目录),如果我单击该子目录,将显示该子目录中的图像。就我而言,我无法从 lambda 内部捕获父目录。我该怎么做?

Dir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",

                                                   QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
for (const QFileInfo &finfo: directory.entryInfoList()) {
    QDir dir(finfo.absoluteFilePath());
    ui->listWidget_dirs->addItem(dir.dirName());
}

//QStringList files = directory.entryList(QDir::Dirs);
//ui->listWidget_dirs->addItems(files);

auto listWidget_images = new QListWidget();//set listwidget to display images
listWidget_images->setMinimumSize(1200,400);
listWidget_images->setViewMode(QListWidget::IconMode);
listWidget_images->setIconSize(QSize(320,240));
listWidget_images->setResizeMode(QListWidget::Adjust);




connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item)
{
    listWidget_images->show();
    listWidget_images->clear();
   /*this is where it is wrong*/ QDir path(directory + '/' + item->text());
    path.setNameFilters({"*.png", "*.jpg"});
    for(const QFileInfo & finfo: path.entryInfoList()){
        QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
        listWidget_images->addItem(item);

    }
});

好吧,我知道怎么做了,这是代码。

QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",

                                                   QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
for (const QFileInfo &finfo: directory.entryInfoList()) {
    QDir dir(finfo.absoluteFilePath());
    ui->listWidget_dirs->addItem(dir.dirName());
}

auto listWidget_images = new QListWidget();//set listwidget to display images
listWidget_images->setMinimumSize(1200,400);
listWidget_images->setViewMode(QListWidget::IconMode);
listWidget_images->setIconSize(QSize(320,240));
listWidget_images->setResizeMode(QListWidget::Adjust);

connect(ui->listWidget_dirs, & QListWidget::itemClicked,[directory,listWidget_images, this](QListWidgetItem *item)
{
    listWidget_images->show();
    listWidget_images->clear();
    // QDir path(item->text());
    QDir dir = directory.absolutePath() + '/' + item->text();
    dir.setNameFilters({"*.png", "*.jpg"});
    for(const QFileInfo & finfo: dir.entryInfoList()){
        QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
        listWidget_images->addItem(item);

    }
});

我只是想留言。确保在你的 lamda 期间你的对象存在,尤其是 listWidget_images。如果你在别处删除它,你的 lamda 会崩溃。我建议稍微更改一下连接线:

来自

connect(ui->listWidget_dirs, & QListWidget::itemClicked,[directory,listWidget_images, this](QListWidgetItem *item)

connect(ui->listWidget_dirs, & QListWidget::itemClicked, listWidget_images, [directory,listWidget_images, this](QListWidgetItem *item)

如果将指针 listWidget_images 放在捕获列表 [] 之前,当来自 listWidget_images 的对象被销毁时,lamda 将被销毁。否则,如果对象不再存在,您的应用程序将在 lamda 的第一行崩溃。