Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to create a QML ListModel from a C++ structure ?
Forum Updated to NodeBB v4.3 + New Features

How to create a QML ListModel from a C++ structure ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 2.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.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by
    #1

    Hi.
    I want to create a QML ListModel from a C++ structure.
    There are more than 100 structure members.
    I thought about adding them one by one to the QList and putting them in the ListModel,
    but adding more than 100 members is a bit of a pain.

    Is there a way to do this that isn't too much trouble?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #8

      If you are using this structure in other code that you don't want to change. Then I would serialize and deserialize the list. It really depends upon how interactive you want it to be. Are you planning on having the changes update in the structure based upon actions by the user? In that case I would go for a model that allows things to be updated and immediately changed in the structure. If you want to just display and update the whole thing at once you can get by using the serialize method. QML ListModel's accept javascript structures:
      {name: "year", value: 22}

      You can build this value as a QVariantList with each entry being QVariantMap. Then return QVariantList to QML. Or you can emit a QVariantMap for each member.

      You won't be able to get around the tedious work this involves. No matter how you do this it will a lot of copy, paste, and edit.

      If you are thinking of changing how you store this in your program you could store the data in a way that is easier to serialize.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #2

        You don't. You implement a QAbstractListModel in C++ and use that in a QML View.

        https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html

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

          What does this structure look like? Is the structure itself repeated? Are you wanting each item to hold 100 properties? Or do you want to create a list that holds each property as a list item? Are the members the same or similar data types? Could they be combined into a list of similar types?

          C++ is a perfectly valid school of magic.

          W 1 Reply Last reply
          0
          • GrecKoG GrecKo

            You don't. You implement a QAbstractListModel in C++ and use that in a QML View.

            https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html

            W Offline
            W Offline
            w.tkm
            wrote on last edited by
            #4

            @GrecKo
            I understand that.
            What I want to ask is how to create that list from a structure.

            1 Reply Last reply
            0
            • fcarneyF fcarney

              What does this structure look like? Is the structure itself repeated? Are you wanting each item to hold 100 properties? Or do you want to create a list that holds each property as a list item? Are the members the same or similar data types? Could they be combined into a list of similar types?

              W Offline
              W Offline
              w.tkm
              wrote on last edited by
              #5

              @fcarney
              Here's a sample structure.

              typedef struct test {
                  unsigned char    year
                  unsigned char    month
                  unsigned char    day
                  unsigned char    hour
                  unsigned char    minute
                  unsigned char    second
                  unsigned short   number
                  unsigned long    id
                  
              //etc...
              }
              

              There are more than 100 such various data members in a single structure
              When I make a list, I'm going to use a type converter to convert it to QString, etc.
              In the end, we want to have a list that can be displayed in a TableView or something similar.

              fcarneyF 1 Reply Last reply
              0
              • W w.tkm

                @fcarney
                Here's a sample structure.

                typedef struct test {
                    unsigned char    year
                    unsigned char    month
                    unsigned char    day
                    unsigned char    hour
                    unsigned char    minute
                    unsigned char    second
                    unsigned short   number
                    unsigned long    id
                    
                //etc...
                }
                

                There are more than 100 such various data members in a single structure
                When I make a list, I'm going to use a type converter to convert it to QString, etc.
                In the end, we want to have a list that can be displayed in a TableView or something similar.

                fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #6

                @w-tkm said in How to create a QML ListModel from a C++ structure ?:

                we want to have a list

                A list of what? I see a structure, but I don't see what you want in your list? Do you have 1000 of these structures? Or do you want to see each member as a separate item in the list?

                C++ is a perfectly valid school of magic.

                W 1 Reply Last reply
                0
                • fcarneyF fcarney

                  @w-tkm said in How to create a QML ListModel from a C++ structure ?:

                  we want to have a list

                  A list of what? I see a structure, but I don't see what you want in your list? Do you have 1000 of these structures? Or do you want to see each member as a separate item in the list?

                  W Offline
                  W Offline
                  w.tkm
                  wrote on last edited by
                  #7

                  @fcarney
                  I want to see each member as a separate item in the list.

                  1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #8

                    If you are using this structure in other code that you don't want to change. Then I would serialize and deserialize the list. It really depends upon how interactive you want it to be. Are you planning on having the changes update in the structure based upon actions by the user? In that case I would go for a model that allows things to be updated and immediately changed in the structure. If you want to just display and update the whole thing at once you can get by using the serialize method. QML ListModel's accept javascript structures:
                    {name: "year", value: 22}

                    You can build this value as a QVariantList with each entry being QVariantMap. Then return QVariantList to QML. Or you can emit a QVariantMap for each member.

                    You won't be able to get around the tedious work this involves. No matter how you do this it will a lot of copy, paste, and edit.

                    If you are thinking of changing how you store this in your program you could store the data in a way that is easier to serialize.

                    C++ is a perfectly valid school of magic.

                    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