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. Qlist of qlist
Qt 6.11 is out! See what's new in the release blog

Qlist of qlist

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

    Hi everybody, I created qvariantlist of qvariantlist like this:
    @ QVariantList DP;
    DP<< 2 << 2300 << 400 << 14.5 << 17.62 << 0 << 0 << 0 << 80;

    QVariantList DC;
    DC<< 3 << 2700 << 35 << 0 << 21.6 << 21.6 << 0 << 2735 << 0;
    
    QVariantList dataList;
    dataList<< DP <<DC  ;@
    

    but my problem is that, when i return the size of the datalist (@qDebug() << dataList.size();@), qdebug shows the number 16, but I want to show 2, because i filled it up with two data :(

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      In your program third list is not declared properly. I think your idea is to create list of QVariantList. It should be something like the following. Try following.

      @ QList<QVariantList> dataList;
      dataList<<DP<<DC;
      qDebug() << dataList.size();@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beh_zad
        wrote on last edited by
        #3

        thanks, it worked. but if i want to pass it to qml what do i do?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beh_zad
          wrote on last edited by
          #4
          i use it: 
          

          @ QQuickView view;

          QQmlContext *myList = view.rootContext();
          myList->setContextProperty("myList", QVariant::fromValue(dataList));
          
          view.setSource(QUrl("Main.qml"));
          
          view.show();@
          

          but in qml i can't use it, for example @myList [0][0]@

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Did you get a chance to look at QQmlListProperty ? This may help you.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beh_zad
              wrote on last edited by
              #6

              is it possible to pass 2d list from c++ to qml ?
              when i used @QVariantList dataList;
              dataList<< DP <<DC ;@ instead of @QList<QVariantList> dataList;
              dataList<<DP<<DC;@ i could to pass it with that codes but in 1d list, for example myList [0] worked but now it doesn't work

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Guys, read the docs.
                << operator for lists appends list elements, not the lists themselves. If you want to create a list of lists you need to explicitly create a variant from the list, eg.

                @
                //this appends DP and DC elements to dataList
                //size of dataList is size of DP + size of DC
                dataList << DP << DC;

                //this creates new variants of type "list" and puts them into dataList
                //size is 2
                dataList << QVariant(DP) << QVariant(DC);
                @

                Btw. QList<QVariantList> doesn't make sense. QVariantList is a typedef for QList<QVariant> so this gives QList<QList<QVariant>>, and what we want is QList<QVariant> where the inner QVariant holds another QList<QVariant>.

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  Thanks Chris. It is typedef as well. Based on this only I suggested him. It depends on the requirement on how to make list of list. I have used it where I wanted list of List only. Based on this only I suggested him to create QList of QVariantList. I don't want QList<Variant>.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    beh_zad
                    wrote on last edited by
                    #9

                    thanks Chris and Dheerendra. i have done it.

                    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