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 n Objects dynamically to populate StackView
Forum Updated to NodeBB v4.3 + New Features

How to create n Objects dynamically to populate StackView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
25 Posts 3 Posters 3.6k 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.
  • fcarneyF fcarney

    @sandro4912 I ran your program. I removed the property index from QuizPage and got rid of "required" from question. It then showed the quiz correctly. When I clicked next question it crashed. Not sure what was wrong there.

    S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #21

    @fcarney
    you are right it does not work anymore. I will check that issue tomorrow and then come back to here.

    1 Reply Last reply
    0
    • S sandro4912

      Of course I could change

      required property var question
      

      to

      property var question
      

      but I thought it would be good to make it required to indicate the page only works with a question supplied. To me that all sounds like a weired bug that index is only available when you don't make a property required.

      so

      required property var index
      

      looks like a workarround.

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

      @sandro4912 said in How to create n Objects dynamically to populate StackView:

      To me that all sounds like a weired bug that index is only available when you don't make a property required.

      That issue on SO was because the OP was not using "delegate" inside of Repeater. The relevance to "required" was due to the repeater not being able to set the property.

      C++ is a perfectly valid school of magic.

      GrecKoG 1 Reply Last reply
      1
      • fcarneyF fcarney

        @sandro4912 said in How to create n Objects dynamically to populate StackView:

        To me that all sounds like a weired bug that index is only available when you don't make a property required.

        That issue on SO was because the OP was not using "delegate" inside of Repeater. The relevance to "required" was due to the repeater not being able to set the property.

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

        @fcarney said in How to create n Objects dynamically to populate StackView:

        That issue on SO was because the OP was not using "delegate" inside of Repeater. The relevance to "required" was due to the repeater not being able to set the property.

        No, it was not because of not using delegate, as a comment said delegate is a default property so you can omit it.
        Repeater { Item {} } is the same as Repeater { delegate : Item {} }.

        The problem was indeed because of the required property. Since Qt 5.15, if a delegate has a required property, the view won't assign the delegate context properties based on the roles of the model. It will only assign roles corresponding to the required properties present in the delegate.

        https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#models

        To get finer control over which roles are accessible, and to make delegates more self-contained and usable outside of views, required properties can be used. If a delegate contains required properties, the named roles are not provided. Instead, the QML engine will check if the name of a required property matches that of a model role. If so, that property will be bound to the corresponding value from the model.
        [...]
        Note: model, index, and modelData roles are not accessible if the delegate contains required properties, unless it has also required properties with matching names.

        That's why you can't access index or modelData if you don't declare them as required property in QuizPage.

        And instead of index, you should use modelData :

        delegate: QuizPage{
            required property var modelData
            question: modelData
        }
        

        Note that QQmlListProperty is not meant to expose C++ data to QML, it is meant to allow the QML to populate a C++ list from QML.
        You should use QList<QObject*>, QVariantList or even better QAbstractListModel if you need a dynamic model. QQmlListProperty does work, but it's not its job.

        S 1 Reply Last reply
        2
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #24

          @GrecKo
          No wonder I was going crazy trying to understand this problem. Thanks for shedding light on this.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1
          • GrecKoG GrecKo

            @fcarney said in How to create n Objects dynamically to populate StackView:

            That issue on SO was because the OP was not using "delegate" inside of Repeater. The relevance to "required" was due to the repeater not being able to set the property.

            No, it was not because of not using delegate, as a comment said delegate is a default property so you can omit it.
            Repeater { Item {} } is the same as Repeater { delegate : Item {} }.

            The problem was indeed because of the required property. Since Qt 5.15, if a delegate has a required property, the view won't assign the delegate context properties based on the roles of the model. It will only assign roles corresponding to the required properties present in the delegate.

            https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#models

            To get finer control over which roles are accessible, and to make delegates more self-contained and usable outside of views, required properties can be used. If a delegate contains required properties, the named roles are not provided. Instead, the QML engine will check if the name of a required property matches that of a model role. If so, that property will be bound to the corresponding value from the model.
            [...]
            Note: model, index, and modelData roles are not accessible if the delegate contains required properties, unless it has also required properties with matching names.

            That's why you can't access index or modelData if you don't declare them as required property in QuizPage.

            And instead of index, you should use modelData :

            delegate: QuizPage{
                required property var modelData
                question: modelData
            }
            

            Note that QQmlListProperty is not meant to expose C++ data to QML, it is meant to allow the QML to populate a C++ list from QML.
            You should use QList<QObject*>, QVariantList or even better QAbstractListModel if you need a dynamic model. QQmlListProperty does work, but it's not its job.

            S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #25

            @GrecKo

            Thanks for clarify. I was very confused how to expose my randomQuestions correctly.

            Now comes the big Question.

            Currently it is like this:

            All Questions are in a class Derived from QSqlTableModel.

            Now for RandomQuestions currently I create with an additional function in that class the QQmlListProperty<Questions>.

            Wouldn't it the best to implement QSortFilterProxModel which picks um the required random questions to expose that to QML?

            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