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. Displaying data from object selected od QListWidget
Qt 6.11 is out! See what's new in the release blog

Displaying data from object selected od QListWidget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a class T with some data and QString NAME. I keep T class objects in a QList<T>.
    First thing I want to do is to print all of the list items by its NAME using QListWidget and its function addItem (using foreach). I believe this solution is not that bad but problem is for my next goal:* I want to print (in other widget) some stored data for T object cliked on that QListWidget*. My first idea is to go through that QList<T> again (using foreach) and compare selected string (from QListWidget) with NAME string fetched one by one from QList<T>. I believe there's the right way because my sounds very bad!

    I have Qt version 4.8.5 and I'm not familiar with QListView etc.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi,

      I would use std::map instead of QList and use the find function

      @
      map<NAME, T> mList;
      T t1, t2, t3;
      t1.setName("T1");
      t2.setName("T2");
      t3.setName("T3");

      mList[t1.name()] = t1;
      mList[t2.name()] = t2;
      mList[t3.name()] = t3;

      // instead of t2.name() use the Name of the selecteditem.
      T listItem = mList.find(t2.name())->second;
      @

      Hope that helps.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Ok thanks but why there is no pure Qt solution? I mean there's std::map used instead of QMap (I see there is no setName() in Qt)

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

          Hi and welcome to devnet,

          Since you didn't share any code, sw0ce used setName as a mean to update the name variable from your class T. It's nothing Qt specific. However you are right about QMap, it would make more sense.

          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

          • Login

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