PDA

View Full Version : How to store/retrieve elements from a QList of QHBoxLayouts ?



Thomas Wrobel
26th November 2009, 20:18
I'm probably getting my pointers all in a muddle here.

I wish to have a set of QHBoxLayouts stored so I can retrieve them and change elements within them. (later Id probably use my own class rather then QHBoxLayouts).

Now, I'm assuming I want to store an array of pointers to the objects, so they can be edited and updated easily. (with the results seen on screen straight away).

However, my code is crashing when I trigger the function to add an element into the array, so I'm probably doing something wrong there.


QHBoxLayout* ArBlipContainer = new QHBoxLayout();
QHBoxLayout* pointerb;
pointerb = ArBlipContainer;
ArBlipContainers->append(pointerb);

I'm pretty sure I don't need the pointer variable at all, but was using it to try to debug.
Either way I get the same result by dumping ArBlipContainer into the append parameter instead.
Commenting out that last line prevents the crash.

ArBlipContainers is defined as;


QList<QHBoxLayout *> *ArBlipContainers; //add the blips known to the system


In the private section of the *.h file.


I apologise if I'm making all sorts of obvious mistakes, I'm migrating from java development, so all this pointer business is new to me :)
(or rather, I guess the ability to not have pointers is)

squidge
26th November 2009, 21:50
Do you allocate the space for ArBlipContainers anywhere? Your saying its a pointer, but don't seem to be pointing it anywhere so att that moment it could be null, or a random value.

Maybe you meant "QList<QHBoxLayout *> ArBlipContainers;", meaning a list of QHBoxLayout object pointers, rather than a pointer to a QList containing a list of QHBoxLayout object pointers.

Thomas Wrobel
26th November 2009, 22:01
Thats, how would I allocate space for it?
I tried;

ArBlipContainers = new QList<QHBoxLayout>;

At the top of the *.cpp file, which is called first.
But that gave a 'no match for operator = ...' error

My indention is just to have a list from which I can store objects I'm adding to the form for later editing.

--
If I change it to ;

QList<QHBoxLayout *> ArBlipContainers;

I get;

error: base operand of `->' has non-pointer type `QList<QHBoxLayout*>'

on the



ArBlipContainers->append(pointerb);

line.

squidge
26th November 2009, 22:35
Have you tried:



ArBlipContainers.append(pointerb);


instead ?

Do note however that you can track down widgets using QObject::findChild and QObject::findChildren, theres no need to duplicate the work yourself.

Thomas Wrobel
27th November 2009, 19:50
Have you tried:



ArBlipContainers.append(pointerb);


instead ?

Do note however that you can track down widgets using QObject::findChild and QObject::findChildren, theres no need to duplicate the work yourself.

bingo! that did the trick.
The GUI kept doing the -> for me, so I assumed it was correct to use. Whats the difference?
(I mean, I understand operators can be defined for specific purpose's, but what should -> be used for in comparison to . ?).

As for findChild, it looks very usefull, but Id rather learn how to handle lists and arrays correctly as I'll naturally be using them a lot anyway.

squidge
27th November 2009, 21:17
When accessing the members of a structure from a pointer, you should use '->'
When accessing the members of a structure via a reference, you should use '.'

QList<QHBoxLayout *> ArBlipContainers; // - reference
QList<QHBoxLayout *> *ArBlipContainers; // - pointer (notice the '*' before the variable name)

Note: '*' is NOT part of the type. It only states that the created variable is a pointer to the type. So:

QList<QHBoxLayout *> *ArBlipContainers, BrBlipContainers;

Create both a pointer and a reference, not two pointers.

Thomas Wrobel
29th November 2009, 00:43
Cheers, thats a most helpful explanation.