Results 1 to 4 of 4

Thread: Add item to diferent listbox on loop

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Add item to diferent listbox on loop

    Good Day All

    I have 20 lis boxes in my ui.
    named:

    in_1
    in_2
    in_3
    ....
    in_20

    now im trying to add items dynamically this is what i would like to do but its not working

    Qt Code:
    1. int i;
    2. for(i=1;i<=20;i++)
    3. {
    4. QString boxtoinput = "in_" + QString::number(i);
    5. ui->boxtoinput->addItem(QString::number(i));
    6. }
    To copy to clipboard, switch view to plain text mode 

    The hope would be the above code will write the number of the box into each box.

    Any ideas of how i would do this
    i know i could write an if statement for each box but then i can genrate the forms dynamically.

    Regards


    Added after 12 minutes:


    Another idea i had would be to loop through using this

    Qt Code:
    1. QList<QListWidget *> allPButtons = this->findChildren<QListWidget *>(boxtoinput);
    To copy to clipboard, switch view to plain text mode 

    that returns
    Qt Code:
    1. QListWidget(0x287daf0, name = "in_5")
    To copy to clipboard, switch view to plain text mode 

    Now with that how does one add add an item to it

    Regards
    Last edited by ShapeShiftme; 17th May 2012 at 02:48.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add item to diferent listbox on loop

    Now with that how does one add add an item to it
    By dereferencing the pointer to your QListWidget to access its member functions.

    Finding the widgets this way is OK if you are do not particularly care about order. You could programmatically create the QListWidgets and insert them into your layout in order to be sure of order.

    Then again 20 lists in the one layout sounds horrible.

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default [Solved] Add item to diferent listbox on loop

    This is how i ended getting it working

    I hope this helps someone

    Qt Code:
    1. int i;
    2. for(i=1;i<=20;i++)
    3. {
    4. QString boxtoinput = "in_" + QString::number(i);
    5. QListWidget* mylst = findChild<QListWidget*>(boxtoinput);
    6. mylst->addItem(QString::number(daycount));
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Solved] Add item to diferent listbox on loop

    It would be much easier and faster for you to use simple QMap< int, QListWidget*> to store the lists and get them from there.

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  3. alignment in listbox
    By sk.qt in forum Qt Programming
    Replies: 7
    Last Post: 27th August 2010, 05:44
  4. Replies: 0
    Last Post: 26th June 2010, 17:13
  5. Replies: 2
    Last Post: 24th March 2010, 10:31

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.