Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Referencing to a QList from a QList<QList<x>> *

Referencing to a QList from a QList<QList<x>> *

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 756 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hi!
    This is surely some kind of stupid question :)
    It is actually a C++ question, but I am getting confused with Qt's behaviour of myList->at(i) and myList[i] and (*myList)[i]

    // allNodeInstances = QList<QList<NodeInstance*>>*
    // so, allNodeInstances is a pointer to a 2D QList
    QList<NodeInstance*> l = (*allNodeInstances)[i];
    qDebug() << "l.length(): " << l.length();
    qDebug() << "(*allNodeInstances)[i].length(): " << (*allNodeInstances)[i].length();
    l.removeOne(ni);
    qDebug() << "l.length(): " << l.length();
    qDebug() << "(*allNodeInstances)[i].length(): " << (*allNodeInstances)[i].length();
    

    the output by running this code is

    l.length():  2
    (*allNodeInstances)[i].length():  2
    l.length():  1
    (*allNodeInstances)[i].length():  2
    

    So, what I actually want to do here is getting a reference to a QList from allNodeInstance-list (at index i).
    The documentation says that the &QList::operator[] returns a modifiable reference. But the output shows, that l isn't a right reference (cuz it doesn't change allNodeInstances).

    QList<NodeInstance*> l = allNodeInstances[i];
    

    causes the following error

    error: conversion from 'QList<QList<NodeInstance*> >' to non-scalar type 'QList<NodeInstance*>' requested
             QList<NodeInstance*> l = allNodeInstances[i];
                                      ~~~~~~~~~~~~~~~~~~^
    

    which is strange, because the [] operator already specifies the index ... surely because allNodeInstances is a pointer to a 2D QList. But if this doesn't work, I don't know, how to achieve what I need.
    So, question is as simple as that: how can I get a working reference to a QList from my 2D QList pointer?
    Thanks for answers!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Is is just paste mistake thats its not a reference ?

      QList<NodeInstance*> & l = allNodeInstances[i];

      note the &
      as the docs shows
      T &QList::operator[](int i)
      it returns ref to type T

      NiagarerN 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Is is just paste mistake thats its not a reference ?

        QList<NodeInstance*> & l = allNodeInstances[i];

        note the &
        as the docs shows
        T &QList::operator[](int i)
        it returns ref to type T

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by Niagarer
        #3

        @mrjj
        Oh yes, you are right, thank you!
        But using allNodeInstances[i] as the reference doesn't work, because it's just a pointer and I get an error, because the compiler still sees a 2D QList

        error: invalid initialization of reference of type 'QList<NodeInstance*>&' from expression of type 'QList<QList<NodeInstance*> >'
                 QList<NodeInstance*> &l = allNodeInstances[i];
                                           ~~~~~~~~~~~~~~~~~~^
        

        so I need to do

        QList<NodeInstance*> &l = (*allNodeInstances)[i];
        l.removeOne(ni);
        

        and it works. I need to specity the new list as a reference (&) and I have to take the data where to pointer points to in order to access the actual lists.

        (&(*C++))->confusing(true)
        

        update
        We can break it down to this:
        after this assignment

        test_struct *sp = new test_struct;
        

        these two are equal:

        sp->a = 12;
        (*sp).a = 12;
        

        So first option is

        QList<NodeInstance*> &l = (*allNodeInstances)[i];
        

        because there is no arrow with the [] operator, the data the pointer points to can only be accessed via the *
        and second (as @mrjj pointed out)

        QList<NodeInstance*> &l = allNodeInstances->operator [](i);
        
        mrjjM 1 Reply Last reply
        0
        • NiagarerN Niagarer

          @mrjj
          Oh yes, you are right, thank you!
          But using allNodeInstances[i] as the reference doesn't work, because it's just a pointer and I get an error, because the compiler still sees a 2D QList

          error: invalid initialization of reference of type 'QList<NodeInstance*>&' from expression of type 'QList<QList<NodeInstance*> >'
                   QList<NodeInstance*> &l = allNodeInstances[i];
                                             ~~~~~~~~~~~~~~~~~~^
          

          so I need to do

          QList<NodeInstance*> &l = (*allNodeInstances)[i];
          l.removeOne(ni);
          

          and it works. I need to specity the new list as a reference (&) and I have to take the data where to pointer points to in order to access the actual lists.

          (&(*C++))->confusing(true)
          

          update
          We can break it down to this:
          after this assignment

          test_struct *sp = new test_struct;
          

          these two are equal:

          sp->a = 12;
          (*sp).a = 12;
          

          So first option is

          QList<NodeInstance*> &l = (*allNodeInstances)[i];
          

          because there is no arrow with the [] operator, the data the pointer points to can only be accessed via the *
          and second (as @mrjj pointed out)

          QList<NodeInstance*> &l = allNodeInstances->operator [](i);
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Niagarer
          yes, sorry missed it was pointer to the outer list.
          so (*allNodeInstances)[i] should always be used.
          you could wrap it in a function for nicer look.

          Also its possible without deference it.
          but its not pretty

          list->operator[](index)
          
          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved