Solved Referencing to a QList from a QList<QList<x>> *
-
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! -
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 -
@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 QListerror: 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 assignmenttest_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);
-
@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 prettylist->operator[](index)