Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget Insert Items
QtWS25 Last Chance

QListWidget Insert Items

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wallacesoh
    wrote on last edited by
    #1

    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:
    @ public:
    QListWidgetItem *item[2];@

    .cpp:
    @ int num = 0;
    for(int i = 0; i < strList.size(); i++)
    {
    if(strList[i] == "ABC")
    {
    ...

            item[num] = new QListWidgetItem();
            item[num]->setText(name);
            ui.listWidget->insertItem(num, item[num]);
            num += 1;
        }
    }@
    

    Output (listWidget):

    bq. ABC 2

    Expected output (listWidget):

    bq. ABC 1
    ABC 2

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      Oke, couple of things. For a QStringList use a iterator instead of the for loop and the [] operator. The [] operator may crash your program bigtime if your loop counter is wrong.
      Also for string comparison use contains("ABC") function.
      For a QListWidget is't not needed to hold the data in a member variable (item list). The QListWidget holds the data, read from there, or when you don't want to do that use a QListView instead and combine with a QAbstractModel subclass to hold your data.
      You also have a memory leak!! When you keep the array of QListWidgetItems and you want to set a new QListWidgetItem you first should delete the old one! Otherwise it is kept in memory.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        There are several things that look fishy:

        Why this item table ? There's not real need for it

        What is the exact content of strList ?

        strList[i] == "ABC" will only be true if the content of that element is Exactly ABC.

        There seems to be some code missing in your if statement so there's no way to know how name is created.

        You can also avoid creating new QListWidgetItem since theres also an insertItem method that takes an int and a QString as argument

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Oke,
          I'll give you a bit of code:
          @
          QStringList::const_iterator IterValue;
          for (IterValue = strList.constBegin(); IterValue < strList.constEnd(); IterValue++)
          {
          if (IterValue->contains("ABC") == true)
          {
          QListWidgetItem * NewItem = new QListWidgetItem;
          NewItem.setText(*IterValue);
          ui->listWidget.addItem(NewItem);
          }
          }
          @

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved