List of lists (or container of containers), is it possible in QT?

  • I was wondering if in QT it is possible to crate a container that can contain other containers of the same kind and other kinds of objects.
    Same question for lists.

    I have been searching on the forum and on the net, but I can't even find anyone speaking of this kind of issue, or maybe I find for the wrong words :(

    Thanks in advance for your help.

  • Lists of lists (or other containers) are the same as lists of anything else. No discussion because it works exactly as one might expect it to.

  • It is not possible in QT (QuickTime) but it is perfectly possible in Qt. Did you like... try it before you ask the question?

  • I have been trying for months and never succeded building a list of lists, and didn't even find any example on the web of lists lof lists, do you know if anywhere I can see an example of simple list of lists that works and that gives no compliling errors?

    Here is my example of a class that should be a list of lists. It is supposed to convert an algebrai expression into a nested list, but when I try to iterate and try to append an object of the same kind as a child to the list I get errors. See the bold in the code below.

    The idea is for instance that the equation a=b+c should be converted into {a,{b,c}}.

    I previously did similar things with php which does not requires variable declaration and it works finely. I did it also with flex, but in Qt I couldn't get any attempt to work, I actually tried many variations, but I must admit I am quite ignorant for what concerns Qt language, thus I decided to ask if it is possible or not, before spending several other months failing :(

    @class LALGExpression : public QList<QString>

    LALG[removed]) {

    LALG[removed]const QString &lalgExpressionText) {
        if(lalgExpressionText.contains("=")) {
            QStringList * tmp = new QStringList(lalgExpressionText.split("="));
            for (int i = 0; i < tmp->size(); ++i) {
                LALGExpression *nextExpr = new LALG[removed]tmp->at(i));
        else if(lalgExpressionText.contains("+")) {
            QStringList * tmp = new QStringList(lalgExpressionText.split("+"));
            for (int i = 0; i < tmp->size(); ++i) {
        else this->append(lalgExpressionText);


  • Sorry I didn't think the code would come unformatted, I don't know how to reformat it here

  • To create a list that contains lists you don't inherit from List<QString> (that create another class that is extending a list of strings), you need to create a QList and as a template parameter pass QList<QString>, so the code will become something like: @QList<QList<QString> > listOfListsOfString; //notice that there is a space between > >@

    LE: since you need a QString in the list a good solution might be a QList<QStringList> listOfListsOfString; (especially if you'll need QStringList's functionality, like join)

  • Oh, thanks, but in this way, will my listOfListsOfString be only 2 steps deep, or with the same declaration will I be allowed to create a listOfListsOfListsOfListsOfListsOfListsOfStrings? I mean with an unfixed dept?

    Thanks in advance

  • You should see an edit link next to your posts to let you fix formatting. There's a code button above the text area that shows how.

    You're declaring your lists as:


    but then you're trying to insert something that isn't a QString into that list. That won't work. For a list of lists of strings you'd use:

    @QList<QList<QString> >@

    but that doesn't seem like it would be flexible enough for what you're really intending to do here. You may want to check the documentation of QVariant which can contain QString or QList<QVariant> (or many other things) and can be used to construct arbitrary trees such as you seem to want.

  • Thanks this QVariant sounds really interesting, I will try to explore it, thanks a lot :)

    ...and yes, I succeded formatting the code above, it wasn't that difficult, doh!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.