How to iterate through a QStringList

I am trying to iterate through two different directories. These two directories are on the same root directory /

.

void MainWindow::loadPlugins()
{
    pluginsDir = QDir(qApp -> applicationDirPath());

#if defined(Q_OS_WIN)
    if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
        pluginsDir.cdUp();
#elif defined(Q_OS_MAC)
    if (pluginsDir.dirName() == "MacOS") {
        pluginsDir.cdUp();
        pluginsDir.cdUp();
        pluginsDir.cdUp();
    }
#endif

    QStringList dirs;
    dirs << "plugins" << "core_plugs";

    QList<QObject *> loadedPlugs;

    for (int i = 0; i < dirs.size(); ++i)
    {
        cout << dirs.at(i).toLocal8Bit().constData() << endl;

        pluginsDir.cd(dirs.at(i).toLocal8Bit().constData());

        foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
            QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
            QObject *plugin = loader.instance();
            qDebug() << "NAME :: " << fileName;
            if (plugin && !(loadedPlugs . contains(plugin))) {
                loadedPlugs << plugin;
                dirs . removeAt(i);
            } else {
                continue;
            }
        }
    }
}

      

I can only enter the first plugin directory on the list dirs << "plugins" << "core_plugs";

. When reordering, the dirs << "core_plugs" << "plugins";

results will be the same and only the first core_plugs directory will be parsed .

Why am I getting this behavior, and how can I make it iterate over both directories.

UPDATE

void MainWindow::loadPlugins()
{
    pluginsDir = QDir(qApp -> applicationDirPath());

#if defined(Q_OS_WIN)
    if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
        pluginsDir.cdUp();
#elif defined(Q_OS_MAC)
    if (pluginsDir.dirName() == "MacOS") {
        pluginsDir.cdUp();
        pluginsDir.cdUp();
        pluginsDir.cdUp();
    }
#endif

    QStringList dirs;
    dirs << "plugins" << "core_plugs";

    QList<QObject *> loadedPlugs;

    for (int i = 0; i < dirs.size(); ++i)
    {
        pluginsDir.cd(dirs.at(i).toLocal8Bit().constData());

        foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
            QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
            QObject *plugin = loader.instance();
            if (plugin && !(loadedPlugs . contains(plugin))) {
                populateRevPlugins(plugin);
                loadedPlugs << plugin;
            }
        }
        qDebug() << pluginsDir . absolutePath();
        pluginsDir.cdUp();
        qDebug() << pluginsDir . absolutePath();
    }
}

      

Following Azeem , I updated my attempt as shown above. I tried cdUp();

after foreach

ie:

foreach() {} pluginsDir.cdUp(); 

      

But he misses. Too late. Sooner than this and I am getting too few iterations. Could you please suggest how can I do this?

Thanks for the answer!

+3


source to share


1 answer


This is how you can iterate over a range based loopQStringList

with C ++ 11 :for

QStringList list { "A", "B", "C" };

for ( const auto& i : list  )
{
    qDebug() << i;
}

      

But I believe your problem is not iteration. Your problem is that you are cd

ing into the directory but not throwing it away i.e. cd("..")

or cdUp()

.

As you said, both of these directories are on the same path, i.e. root /

. So, after processing in a nested loop, you need to go back to the root path to access another directory.

You can just check the result of the function cd

whether it was successful or not. This is a boolean function. But, in your case, there is no error handling. With error handling, it should be something like this:

if ( dir.cd( path ) )
{
    // Do something here...
}

      

You don't need to get dirs.at(i).toLocal8Bit().constData()

for the function cd

. Required QString

.

Here is his signature:

bool QDir::cd( const QString& dirName );

      



Hint: You can also use QDirIterator to iterate over a directory.

Below is a complete working example.

I followed this directory structure for a demonstration:

C:\Test
-- A
---- a.txt
-- B
---- b.txt

      

Example:

#include <QStringList>
#include <QDebug>
#include <QDir>

int main( int argc, char *argv[] )
{
    Q_UNUSED( argc );
    Q_UNUSED( argv );

    const QString     root { "C:/Test" };
    const QStringList dirs { "A", "B" };

    QDir rootDir { root };
    qDebug() << "Root Path:" << rootDir.absolutePath();
    qDebug() << "Root List:" << rootDir.entryList( QDir::Dirs ) << '\n';

    for ( const auto& dir : dirs  )
    {
        if ( rootDir.cd( dir ) )
        {
            qDebug() << "Dir Path:" << rootDir.absolutePath();
            qDebug() << "Dir List:" << rootDir.entryList( QDir::Files ) << '\n';
        }

        rootDir.cdUp();
        qDebug() << "cding...";
        qDebug() << "Dir Path:" << rootDir.absolutePath() << '\n';
    }

    return EXIT_SUCCESS;
}

      

Here's the output:

enter image description here

+4


source







All Articles