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. QListView / QListWidget with custom data
Forum Updated to NodeBB v4.3 + New Features

QListView / QListWidget with custom data

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.8k 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.
  • H Offline
    H Offline
    hfhc2
    wrote on last edited by
    #1

    Hello all,

    I am currently adding a GUI to a larger project. So far I used the QGraphicsScene to render a large network and I must say that I am really impressed with it:)

    However, I have a problem with the QListWidget / QListView: I want to display data obtained from another part of the application. Each entry basically consists of a pointer to an object. I want to have an entry in the widget for each pointer with a name which I derive from the data and present as a QString.
    Otherwise, I only need the pointers. I want to be able to select one or multiple items by passing a list of pointers to the widget and I want to notify other widgets of a changed selection, where the other widgets once again only need the pointers which are currently selected. What is the most efficient (with respect to code size and run/compile time) way to implement this using either a QListWidget or a QListView (I don't really care which)?

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

      Hi and welcome to devnet,

      Since you already have a list of object, you could implement a QAbstractItemList model on top of your data and thus use a QListView. You can re-use the model on your other widgets needing to access the data.

      Hope it helps

      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
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I agree with SGaist. Create a QAbstractItemModel adaptor on your underlying data store. If all you need is a list, then by all means use a QAbstractListModel as its base, but I would stick with advertising the API as exposing a QAbstractItemModel. That way, the fact that you're using a list remains an implementation detail, easy to change when needed.

        Note that while it is possible to use QAbstractItemModel's interface for other purposes than model/view, I really recommend against doing that. The API is too generic and too cluncky to be nice to use in other contexts. Instead, provide specialized API on your actual data store.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hfhc2
          wrote on last edited by
          #4

          Well, I realize now that my question might have been a little vague. The situation is the following: Lets assume I have the following data model:

          @
          int data[] = {0, 1, 2, 3};

          typedef int Data;

          class Handler
          {
          public:
          Data* by_index(int i) const
          {
          return data + i;
          }

          int num_elems() const
          {
          return 4;
          }
          } my_handler;

          QString to_string(Data *data)
          {
          return QString("Data item %1").arg(*data);
          }
          @

          Now I want to display the data in a QListView. For each item I only want to display the QString obtained from to_string and no other formatting. I would also like to have a signal dataChanged(QList<Data*> currently_selected_data) and a function select_data(QList<Data*> to_be_selected)
          Could you give me some more concrete advice on how to achieve this functionality?

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

            Since it's a list, return num_elems for rowCount() and use to_string when data() is called for DisplayRole

            Are you sure you need these two signals ? Item views already provide a selection model

            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
            • H Offline
              H Offline
              hfhc2
              wrote on last edited by
              #6

              Well, I want other components to react to the changed selection. But the normal signal in QAbstractItemView works with these crazy QItemSelection objects which are QLists of QItemSelectionRanges and so on. The problem is that I want a map from the selection as stored in Qt to my data.
              I don't know or care about the way the QItemView stores the selection internally, but I need to map the selection to my data objects.

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

                Well, these signals gives you QModelIndexes that you can use to get your data through the model so you don't have to worry about handling the selection.

                Isn't that what you need ?

                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
                • H Offline
                  H Offline
                  hfhc2
                  wrote on last edited by
                  #8

                  Well, what is the best way to get a data entries from a QModelIndex? There is an internal pointer (a void*). Can I use that pointer or can I somehow get a map from the QModelIndexes to my Data entries without having to define that map myself?

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

                    That's where Andre's specialized API suggestion kicks in, you can provide a function like

                    @Data dataForIndex(const QModelIndex &index) const
                    {
                    if (!index.isValid())
                    return Data();
                    return _handler.by_index(index.row());
                    }@

                    You can do something similar for a range of indexes

                    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