Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string from QStringList if string value is part of other string

Tags:

string

qt

I want to remove a string from a QStringList (folders in the code below) if it is included in another string from the same list.

Example: "/tmp/a /tmp/b /tmp/a/aa /tmp/c /tmp/a/aa/aaa /tmp/d"

I want to remove the first and third string as they are included in the fifth. I know how to do it with grep in bash, but how would I do it with Qt?

void MainWindow::on_toolButtonSourceFolders_clicked()
{
    QString startDir = lineEditStartFolder->text();
    QFileDialog* folderDialog = new QFileDialog(this);
    folderDialog->setDirectory(lineEditStartFolder->text());
    folderDialog->setFileMode(QFileDialog::Directory);
    folderDialog->setOption(QFileDialog::DontUseNativeDialog, true);
    folderDialog->setOption(QFileDialog::ShowDirsOnly, true);
    folderDialog->setOption(QFileDialog::DontResolveSymlinks, true);
    QListView *folderList = folderDialog->findChild<QListView*>("listView");
    if (folderList) {
        folderList->setSelectionMode(QAbstractItemView::MultiSelection);
    }
    QTreeView *folderTree = folderDialog->findChild<QTreeView*>();
    if (folderTree) {
        folderTree->setSelectionMode(QAbstractItemView::MultiSelection);
    }

    folderDialog->exec();
    QStringList folders = folderDialog->selectedFiles();
    if (!folders.isEmpty())
            listWidget->addItems(folders);
}

The complete code is available at https://github.com/FluxFlux/qdir2mod

like image 231
FluxFlux Avatar asked Dec 07 '25 03:12

FluxFlux


2 Answers

As indicated in the comments, since the maximum number of directories is 20, I would not bother optimizing the algorithm, hence I would just go with the simplest:

QStringList folders = folderDialog->selectedFiles();
QStringList outputFolders = folders;
foreach (const QString &folder, folders) {
    foreach (const QString &f, folders) {
        if (f.contains(folder))
            outputFolders.removeOne(folder);
    }
}

You could also avoid the temporary copy, but then again, it would make the code more complex which is not worth for 20 "folders".

Also, please note that "folder" is a GUI term. What you are referring to is files and directories which is more universal. It is better to use the proper term and not get stuck with GUI terms, only.

like image 88
lpapp Avatar answered Dec 08 '25 21:12

lpapp


With a slight modification I've solved the problem:

QStringList folders = folderDialog->selectedFiles();
QStringList outputFolders = folders;
foreach (const QString &folder, folders) {
    foreach (const QString &f, folders) {
        const QString &cfolder = (folder + "/");
        if (f.contains(cfolder))
            outputFolders.removeOne(folder);
    }
}
if (!outputFolders.isEmpty())
        listWidget->addItems(outputFolders);
like image 45
FluxFlux Avatar answered Dec 08 '25 19:12

FluxFlux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!