Results 1 to 3 of 3

Thread: QListWidget Insert Items

  1. #1

    Default QListWidget Insert Items

    I am trying to add two items into my QListWidget dynamically. However, the following codes only allow me to add only the last item into the list. strList.size() contains 4 items. Assuming name contains "ABC 1" and "ABC 2".

    Is my loop incorrect? Or is my method of adding items into the listWidget wrong?

    .h:

    Qt Code:
    1. public:
    2. QListWidgetItem *item[2];
    To copy to clipboard, switch view to plain text mode 

    .cpp:

    Qt Code:
    1. int num = 0;
    2. for(int i = 0; i < strList.size(); i++)
    3. {
    4. if(strList[i] == "ABC")
    5. {
    6. ...
    7.  
    8. item[num] = new QListWidgetItem();
    9. item[num]->setText(name);
    10. ui.listWidget->insertItem(num, item[num]);
    11. num += 1;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Output (listWidget):

    ABC 2
    Expected output (listWidget):

    ABC 1
    ABC 2

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListWidget Insert Items

    Aside from the fact that your code will blow up or exhibit undefined behavior if there happens to be more than 2 items in the stringlist that match "ABC" and that fact that you obviously haven't shown all of it (like, where is "name" defined, for instance? Must be those unimportant "..." lines) what you do show appears to be OK. If you get the same behavior using the addItem() method instead of insertItem(), then you are doing something somewhere else that messes up the list.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QListWidget Insert Items

    wallacesoh

    In class .h file, instead of
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    do this,
    Qt Code:
    1. QList<QListWidgetItem*> item;
    To copy to clipboard, switch view to plain text mode 
    In .cpp file, do this
    Qt Code:
    1. for(int i = 0; i < strList.size(); i++)
    2. {
    3. item.append(new QListWidgetItem());
    4. ....
    5. ....
    6. }
    7. // Access the item using at operator like this item.at(i);
    To copy to clipboard, switch view to plain text mode 
    This way, you can add QListWidgetItems dynamically without need to predefine the number of items.
    Last edited by rawfool; 24th February 2014 at 06:51.

Similar Threads

  1. Replies: 4
    Last Post: 5th June 2013, 15:24
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. How to insert QProgressbar in a QListWidget?
    By mismael85 in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2010, 06:54
  4. how to insert items in tableWidget
    By roncriss in forum Newbie
    Replies: 3
    Last Post: 24th July 2010, 17:10
  5. Insert an item in an horizontal QListWidget
    By satoshi in forum Qt Programming
    Replies: 8
    Last Post: 19th May 2009, 17:33

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.