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. QListWidgetItem storing a multi value field

QListWidgetItem storing a multi value field

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.0k 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
    WhatIf
    wrote on last edited by
    #1

    I have a QListWidget that I add QListWidgetItem(s) to and remove from. The problem I'm trying to find a solution to is each QListWidgetItem adds a QString to the QListWidget. I still want it to do that but also add and ID associated with each QString in the background. So that if I want to delete any QString from the QListWidget, its associated ID is deleted as well.

    I thought about adding std::map, storing ID and string in it before adding the QString to the QListWidget but if I add similar QString, how would I know which QString is associate with which ID to remove?

    Is there an easier solution?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Something like this ?
      http://stackoverflow.com/questions/7136818/can-i-store-some-user-data-in-every-item-of-a-qlistwidget
      You could store an ID for your map.

      Its a QVariant so you can also use your own type/class/struct if you register it,
      http://stackoverflow.com/questions/24362946/how-can-i-cast-a-qvariant-to-custom-class
      Q_DECLARE_METATYPE(CustomType) part.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WhatIf
        wrote on last edited by WhatIf
        #3

        I modified the code in the link you provided. The code is working fine but occasionally I'm getting segmentation fault and the app terminates. Here is my code:

                QListWidgetItem* lineItem = new QListWidgetItem(name, ui->listWidget);
                lineItem->setData(Qt::UserRole, ID);
                lineItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
                lineItem->setCheckState(Qt::Unchecked);
                ui->listWidget->addItem(lineItem);
        

        Will using a parent ( ui->listWidget) in the QListWidgetItem cause problems?

        to retrieve ID, I use the following code:

            for(int i = 0; i < ui->listWidget->count(); ++i)
            {
                QListWidgetItem* currentItem = ui->listWidget->item(i); 
                if (currentItem != 0) {
                     QVariant id = currentItem->data(Qt::UserRole);
                     saveID(id.toString());
                  }
             }
        

        The code compiles and run but will it cause program failure?

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

          Hi,

          What does a run through the debugger tell you about that crash ?

          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
          1

          • Login

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