Results 1 to 4 of 4

Thread: How to iterate a QList of structs and modify struct elements directly?

  1. #1
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy 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:
    Qt Code:
    1. struct buttonsStruct
    2. {
    3. QWidget *w;
    4. };
    5.  
    6. QPushButton *m_fireButton;
    7. QPushButton *m_iceButton;
    To copy to clipboard, switch view to plain text mode 

    In my cpp file, create two struct objects and add them to a QList.
    Qt Code:
    1. buttonsStruct b1 = {m_fireButton, "Fire", m_tabFire};
    2. buttonsStruct b2 = {m_iceButton, "Ice", m_tabIce};
    3.  
    4. QList<buttonsStruct*> buttonsQList;
    5. buttonsQList.append(&b1);
    6. buttonsQList.append(&b2);
    To copy to clipboard, switch view to plain text mode 

    I iterate through out each QList struct, creating a new QPushButton.

    Qt Code:
    1. QMutableListIterator<buttonsStruct*> it(buttonsQList);
    2. while (it.hasNext())
    3. {
    4. buttonsStruct *bs = it.next();
    5.  
    6. bs->pb = new QPushButton(m_centralWidget);
    7. (bs->pb)->setGeometry(QRect(xpos, ypos, 85, 25));
    8. (bs->pb)->setText(bs->s);
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,444
    Thanked 269 Times in 264 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to iterate a QList of structs and modify struct elements directly?

    I put something in m_fireButton with line #6:
    Qt Code:
    1. bs->pb = new QPushButton(m_centralWidget);
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,444
    Thanked 269 Times in 264 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to iterate a QList of structs and modify struct elements directly?

    Quote Originally Posted by bobbayribs View Post
    I put something in m_fireButton with line #6:
    Qt Code:
    1. bs->pb = new QPushButton(m_centralWidget);
    To copy to clipboard, switch view to plain text mode 
    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.

  5. The following user says thank you to Lesiok for this useful post:

    bobbayribs (27th December 2017)

Similar Threads

  1. Can't compile QList<struct>
    By mikrocat in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2015, 06:04
  2. QList<struct>
    By Axsis in forum Newbie
    Replies: 11
    Last Post: 12th October 2015, 07:48
  3. How to fill a QList<struct> ?
    By falconium in forum Newbie
    Replies: 1
    Last Post: 1st March 2011, 21:59
  4. Use QXmlStreamWriter to modify existing elements
    By rhf417 in forum Qt Programming
    Replies: 8
    Last Post: 25th June 2009, 02:23
  5. memset struct with QString elements
    By Timewarp in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 01:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.