How to iterate a QList of structs and modify struct elements directly?
Hi, I'm trying to iterate through a QList of structs and modify elements within the struct, but can't figure it out.
My header looks like this:
Code:
struct buttonsStruct
{
};
In my cpp file, create two struct objects and add them to a QList.
Code:
buttonsStruct b1 = {m_fireButton, "Fire", m_tabFire};
buttonsStruct b2 = {m_iceButton, "Ice", m_tabIce};
QList<buttonsStruct*> buttonsQList;
buttonsQList.append(&b1);
buttonsQList.append(&b2);
I iterate through out each QList struct, creating a new QPushButton.
Code:
QMutableListIterator<buttonsStruct*> it(buttonsQList);
while (it.hasNext())
{
buttonsStruct *bs = it.next();
(bs
->pb
)->setGeometry
(QRect(xpos, ypos,
85,
25));
(bs->pb)->setText(bs->s);
...
}
When I try to access m_fireButton, I get a crash, and checking the memory address, I get 0x0.
Isn't bs->pb the same address as m_fireButton or m_iceButton? I'm modifying my struct elements directly in the iterator, no?
I looked through the documentation and found QMutableListIterator::QMutableListIterator(QList<T > & list)
so the QList is passed by reference. I can't see where I went wrong here. Any help?
Re: How to iterate a QList of structs and modify struct elements directly?
1. I can't see where You put something in m_fireButton.
2. You create b1 and b2 on heap and then You save its addresses. You have to create this objects with new. Think why.
Re: How to iterate a QList of structs and modify struct elements directly?
I put something in m_fireButton with line #6:
bs->pb is a pointer to m_fireButton.
At least I thought so, I think a copy is occurring somewhere instead of the pointer itself.
Re: How to iterate a QList of structs and modify struct elements directly?
Quote:
Originally Posted by
bobbayribs
I put something in m_fireButton with line #6:
bs->pb is a pointer to m_fireButton.
At least I thought so, I think a copy is occurring somewhere instead of the pointer itself.
1. After this line bs->pb is pointing to new QPushButton no to m_fireButton.
2. bs->pb is NOT a pointer to m_fireButton. It is a pointer to QPushButton with the same value as m_fireButton.
Sorry, these are the basics of C ++ - nothing to Qt.