Search code examples
c++qtqlist

How to use declare custom typedef for QListIterator


I want to use QListIterator, example shown here: https://doc.qt.io/archives/qt-4.8/qlistiterator.html

I have declared a typedef for the list containing QStringList:

    typedef QList<QStringList> slstStringList;

I want to declare a type that is an iterator for the above, I first tried:

    typedef QListIterator<slstStringList> slstIterator;

However that didn't work, on looking at the link I posted above I changed it to:

    typedef QListIterator<QStringList> slstIterator;

The compiler doesn't like this either, I get:

    error: no matching function for call to 'QListIterator<QStringList>::QListIterator()'

What have I done wrong?

Here is an example of where I have tried to use both:

    for( slstIterator it(clsResetCfg::mslstWiFi); it.hasNext(); ) {
        QStringList slstDetails = it.next();
        ...
    }

The above works and compiles ok, the error was in a static initialisation of an iterator where I had not specified the static list member, I've changed this now to:

    slstStringList clsResetCfg::mslstWiFi;
    slstIterator clsResetCfg::msitWiFi(clsResetCfg::mslstWiFi);

Now all is right in the world :)


Solution

  • typedef QListIterator<QStringList> slstIterator;
    

    This is correct, but you need to pass a reference to the list itself to the constructor once you instantiate it:

    slsIterator it{myList};