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. trying to create list (docs seem in error)
Forum Updated to NodeBB v4.3 + New Features

trying to create list (docs seem in error)

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 739 Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I'm trying to create a list, but am getting errors. I tried the code in the pic below -- this came straight from the docs:
    error.PNG
    Here's how I'm trying to use it - I may have my own errors in how I'm trying to use color:

    property list<color> rectColors: [Qt.red, Qt.green, Qt.blue, Qt.yellow, Qt.orange]
    Repeater {
        id: rects
        model: rectColors
        Rectangle {
            width: mainWindow.width 
            height: 100
            color: rectColors[index]
        }
    }
    

    Any ideas on what's going on here? Thanks...

    JoeCFDJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @JoeCFD OK, thanks...it was worth a try.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #6

      @mzimmers What @JoeCFD said is only true for Qt 5. In Qt 6 in can also hold basic value types as mentioned by the doc you linked.

      Qt Creator is bit confused in its linting but the code actually runs.

      There is a problem in your code though. Qt.red and its friends are not actual colors but enums, you can't cast them to color.

      You could use a QColor from C++, Qt.rgba(), a SVG color name or hex color code:
      property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"]

      1 Reply Last reply
      4
      • mzimmersM mzimmers

        Hi all -

        I'm trying to create a list, but am getting errors. I tried the code in the pic below -- this came straight from the docs:
        error.PNG
        Here's how I'm trying to use it - I may have my own errors in how I'm trying to use color:

        property list<color> rectColors: [Qt.red, Qt.green, Qt.blue, Qt.yellow, Qt.orange]
        Repeater {
            id: rects
            model: rectColors
            Rectangle {
                width: mainWindow.width 
                height: 100
                color: rectColors[index]
            }
        }
        

        Any ideas on what's going on here? Thanks...

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        @mzimmers https://stackoverflow.com/questions/26733011/how-to-declare-list-property-in-qml

        mzimmersM 1 Reply Last reply
        1
        • JoeCFDJ JoeCFD

          @mzimmers https://stackoverflow.com/questions/26733011/how-to-declare-list-property-in-qml

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          @JoeCFD:

          1. so, the doc is wrong?
          2. according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?
          JoeCFDJ 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @JoeCFD:

            1. so, the doc is wrong?
            2. according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?
            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #4

            @mzimmers
            https://doc.qt.io/qt-5/qml-list.html
            A list can only store QML objects, and cannot contain any basic type values.
            (To store basic types within a list, use the var type instead.)

            mzimmersM JKSHJ 2 Replies Last reply
            0
            • JoeCFDJ JoeCFD

              @mzimmers
              https://doc.qt.io/qt-5/qml-list.html
              A list can only store QML objects, and cannot contain any basic type values.
              (To store basic types within a list, use the var type instead.)

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #5

              @JoeCFD OK, thanks...it was worth a try.

              GrecKoG 1 Reply Last reply
              0
              • mzimmersM mzimmers has marked this topic as solved on
              • mzimmersM mzimmers

                @JoeCFD OK, thanks...it was worth a try.

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #6

                @mzimmers What @JoeCFD said is only true for Qt 5. In Qt 6 in can also hold basic value types as mentioned by the doc you linked.

                Qt Creator is bit confused in its linting but the code actually runs.

                There is a problem in your code though. Qt.red and its friends are not actual colors but enums, you can't cast them to color.

                You could use a QColor from C++, Qt.rgba(), a SVG color name or hex color code:
                property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"]

                1 Reply Last reply
                4
                • JoeCFDJ JoeCFD

                  @mzimmers
                  https://doc.qt.io/qt-5/qml-list.html
                  A list can only store QML objects, and cannot contain any basic type values.
                  (To store basic types within a list, use the var type instead.)

                  JKSHJ Online
                  JKSHJ Online
                  JKSH
                  Moderators
                  wrote on last edited by
                  #7

                  @JoeCFD said in trying to create list (docs seem in error):

                  A list can only store QML objects, and cannot contain any basic type values.

                  Not true since Qt 6.4. See https://doc.qt.io/qt-6/qml-list.html#using-the-list-type

                  (To store basic types within a list, use the var type instead.)

                  Using var prevents your QML code from getting compiled to C++ (see https://www.qt.io/blog/the-new-qtquick-compiler-technology ). Always prefer the specific type where possible.

                  @mzimmers said in trying to create list (docs seem in error):

                  1. so, the doc is wrong?

                  No, Qt Creator is wrong: https://bugreports.qt.io/browse/QTCREATORBUG-28238 It will be fixed in Qt Creator 10.0 (the beta is currently available from the online installer)

                  Your code will run fine with Qt 6.4 and newer.

                  1. according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?

                  False 😊 But @GrecKo is right: Use "red" instead of Qt.red

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  JoeCFDJ 1 Reply Last reply
                  3
                  • JKSHJ JKSH

                    @JoeCFD said in trying to create list (docs seem in error):

                    A list can only store QML objects, and cannot contain any basic type values.

                    Not true since Qt 6.4. See https://doc.qt.io/qt-6/qml-list.html#using-the-list-type

                    (To store basic types within a list, use the var type instead.)

                    Using var prevents your QML code from getting compiled to C++ (see https://www.qt.io/blog/the-new-qtquick-compiler-technology ). Always prefer the specific type where possible.

                    @mzimmers said in trying to create list (docs seem in error):

                    1. so, the doc is wrong?

                    No, Qt Creator is wrong: https://bugreports.qt.io/browse/QTCREATORBUG-28238 It will be fixed in Qt Creator 10.0 (the beta is currently available from the online installer)

                    Your code will run fine with Qt 6.4 and newer.

                    1. according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?

                    False 😊 But @GrecKo is right: Use "red" instead of Qt.red

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #8

                    @JKSH You two guys are right. a list in Qt6 is different.

                    1 Reply Last reply
                    0
                    • mzimmersM mzimmers has marked this topic as unsolved on
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #9

                      Thanks for the clarification on this. I downloaded Creator 10 and as @JKSH said, the error disappeared. Here's the working code:

                      Column {
                          Repeater {
                              id: repeater
                              property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"]
                              model: rectColors
                              Rectangle {
                                  width: mainWindow.width
                                  height: 100
                                  color: repeater.rectColors[index]
                              }
                          }
                      }
                      

                      Seems to work fine. Now I can ask my real question (in another topic). Thanks again.

                      1 Reply Last reply
                      0
                      • mzimmersM mzimmers has marked this topic as solved on
                      • GrecKoG Offline
                        GrecKoG Offline
                        GrecKo
                        Qt Champions 2018
                        wrote on last edited by
                        #10

                        @mzimmers

                        You are not using Repeater the intended way. You are not supposed to acces the raw model from the delegates, let the Repeater provide you the data.

                        Change

                        color: repeater.rectColors[index]
                        

                        with

                        color: modelData
                        
                        mzimmersM 1 Reply Last reply
                        2
                        • GrecKoG GrecKo

                          @mzimmers

                          You are not using Repeater the intended way. You are not supposed to acces the raw model from the delegates, let the Repeater provide you the data.

                          Change

                          color: repeater.rectColors[index]
                          

                          with

                          color: modelData
                          
                          mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #11

                          @GrecKo noted; thanks.

                          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