跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  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

已排程 已置頂 已鎖定 已移動 Unsolved QML and Qt Quick
25 貼文 3 Posters 3.7k 瀏覽 1 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • 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 離線
    S 離線
    sandro4912
    寫於 最後由 編輯
    #21

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

    1 條回覆 最後回覆
    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 離線
      fcarneyF 離線
      fcarney
      寫於 最後由 編輯
      #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 條回覆 最後回覆
      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 離線
        GrecKoG 離線
        GrecKo
        Qt Champions 2018
        寫於 最後由 編輯
        #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 條回覆 最後回覆
        2
        • fcarneyF 離線
          fcarneyF 離線
          fcarney
          寫於 最後由 編輯
          #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 條回覆 最後回覆
          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 離線
            S 離線
            sandro4912
            寫於 最後由 編輯
            #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 條回覆 最後回覆
            0

            • 登入

            • Login or register to search.
            • 第一個貼文
              最後的貼文
            0
            • 版面
            • 最新
            • 標籤
            • 熱門
            • 使用者
            • 群組
            • 搜尋
            • Get Qt Extensions
            • Unsolved