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. Accessing property by name or by READ function ?
QtWS25 Last Chance

Accessing property by name or by READ function ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
propertyqml
6 Posts 4 Posters 773 Views
  • 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.
  • M Offline
    M Offline
    Mixlu
    wrote on last edited by Mixlu
    #1

    Hello,

    I have a list of model in a property declared as follow :

    Q_PROPERTY(QQmlListProperty<MyModel> listModel READ getListModel NOTIFY listModelChanged)
    

    I use this property in QML in a ListView as follow (only relevant code is shown):

    ListView {
        model: Backend.getListModel()
    }
    

    My question is, should I declare my ListView model with the READ function (model: Backend.getListModel()) or just with the property name (model: Backend.listModel) ?
    Is there any difference between these two options ?

    PS : I am working with Qt 5.14.2 with MinGW (windows)

    KroMignonK 1 Reply Last reply
    0
    • M Mixlu

      Hello,

      I have a list of model in a property declared as follow :

      Q_PROPERTY(QQmlListProperty<MyModel> listModel READ getListModel NOTIFY listModelChanged)
      

      I use this property in QML in a ListView as follow (only relevant code is shown):

      ListView {
          model: Backend.getListModel()
      }
      

      My question is, should I declare my ListView model with the READ function (model: Backend.getListModel()) or just with the property name (model: Backend.listModel) ?
      Is there any difference between these two options ?

      PS : I am working with Qt 5.14.2 with MinGW (windows)

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @Mixlu said in Accessing property by name or by READ function ?:

      My question is, should I declare my ListView model with the READ function (model: Backend.getListModel()) or just with the property name (model: Backend.listModel()) ?
      Is there any difference between these two options ?

      Yes, there is a big difference!
      By using model: Backend.getListModel() the affectation will be static, model will be set once at Item load.
      With model: Backend.listModel the affectation will be dynamic, on each listModelChanged signal, model will re-read Backend.getListModel().

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      M 1 Reply Last reply
      4
      • KroMignonK KroMignon

        @Mixlu said in Accessing property by name or by READ function ?:

        My question is, should I declare my ListView model with the READ function (model: Backend.getListModel()) or just with the property name (model: Backend.listModel()) ?
        Is there any difference between these two options ?

        Yes, there is a big difference!
        By using model: Backend.getListModel() the affectation will be static, model will be set once at Item load.
        With model: Backend.listModel the affectation will be dynamic, on each listModelChanged signal, model will re-read Backend.getListModel().

        M Offline
        M Offline
        Mixlu
        wrote on last edited by
        #3

        @KroMignon Ok I see, it's clear. Thank's for your reply!

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

          Note that in general you should not mark your properties' READ functions as slot or Q_INVOKABLE, just make it a simple public function.
          This way you can only access it by name and have a clear QML api.

          fcarneyF 1 Reply Last reply
          4
          • GrecKoG GrecKo

            Note that in general you should not mark your properties' READ functions as slot or Q_INVOKABLE, just make it a simple public function.
            This way you can only access it by name and have a clear QML api.

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

            @GrecKo This is interesting as the refactor option in creator always makes the READ function a slot. Thanks for the insight.

            C++ is a perfectly valid school of magic.

            GrecKoG 1 Reply Last reply
            0
            • fcarneyF fcarney

              @GrecKo This is interesting as the refactor option in creator always makes the READ function a slot. Thanks for the insight.

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

              @fcarney This will be configurable in Qt Creator 4.15 : https://bugreports.qt.io/browse/QTCREATORBUG-15779 https://codereview.qt-project.org/c/qt-creator/qt-creator/+/310832

              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