Results 1 to 3 of 3

Thread: How to create an array of objects??

  1. #1
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to create an array of objects??

    Hi,

    I would like to know, if I can create an object array with the objects that I create with QDesigner.

    For example, I put 20 of QLabel object; but QDesigner creates them as label1, label2, ..., label20; all with a different name. Of course, this makes harder to reach the objects. I want to put them in an array like QLabel label[20], so that I can reach all of them just by changing index. Can I do that?

    (Related post was answered as "No" about 2.5 years ago, but I want to know if there is a change with Qt 4)

    Related post: http://www.qtcentre.org/threads/12866-how-we-convert-no-of-pointer-objects-into-a-single-array

    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create an array of objects??

    It's still the same as far as I'm aware. You can of course create the array in code and associate the objects at runtime, either by direct assignment (x[i] = label1) or using the methods of the main QObject and searching for them (QObject::children). If the labels in particular belong to a layout, the later is pretty easy as the only objects in the layout with be your labels.

  3. The following user says thank you to squidge for this useful post:

    dominate (22nd January 2011)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to create an array of objects??

    Alternate way is that you create the array/QVector/QList of QLabel (and of course initialize the QLabels) in code (member in the same class as ui or ui*) and you can then add*** them to the rest of the form created with designer.

    ***you won't see them in designer, the add is done also in code, just add them to the layout you need or create a layout for them and parent the layout with the appropriate Widget from the ui.

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

    dominate (22nd January 2011)

Similar Threads

  1. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 02:20
  2. QtScript: Passing an array of objects to C++
    By Plow in forum Qt Programming
    Replies: 0
    Last Post: 16th April 2010, 12:53
  3. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11
  4. Replies: 0
    Last Post: 25th June 2008, 18:36
  5. Replies: 2
    Last Post: 16th April 2008, 10:31

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.