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. Q_PROPERTY c++/QML

Q_PROPERTY c++/QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 4 Posters 2.5k 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.
  • D Offline
    D Offline
    Delta_sim
    wrote on last edited by
    #1

    Hello,

    I would like to know if it's safe to use a lot of Q_PROPERTY in a qt quick application.
    My database is in c++ and I have to use this database in QML (most of the time read-only).

    Thanks for your help.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      Yes it is safe. QML uses only Qt properties to achieve its functionality. Everything you set (like color: "red" in a Rectangle) is a Q_PROPERTY.

      (Z(:^

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

        Your idea is to expose the values present in Database. You would like to read them & pass the values to QML. Hope this is right. As @sierdzio suggested already, you can use Q_PROPERTY. It is designed for this. You can also expose the list to QML side as Q_PROPERTY instead of individual Q_PROPERTY.

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

        1 Reply Last reply
        1
        • D Offline
          D Offline
          Delta_sim
          wrote on last edited by
          #4

          Ok thanks for your replies.
          @dheerendra you suggested to expose the list to QML and it's a good idea, QQmlListProperty will do the job ? Or I should use an other type ?

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

            QQmlListProperty is made to expose a list so that can be populated from QML. That's not what you want.
            To expose a list of data, use QAbstractListModel (read http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass). If your data is static, you could use simpler classes like QStringList, QVariantList (composed of QVariantMap for example) or QObjectList.

            1 Reply Last reply
            1
            • D Offline
              D Offline
              Delta_sim
              wrote on last edited by
              #6

              Thanks @GrecKo , I will do that

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Delta_sim
                wrote on last edited by
                #7

                @GrecKo as you said I expose a QVariantList like this:

                //c++
                Q_PROPERTY(QList<QVariant> gav_GlobalDb READ gav_get_GlobalDb WRITE gv_set_GlobalDb)
                
                QList<QVariant> gav_get_GlobalDb(void);
                void gv_set_GlobalDb(QList<QVariant>  oav_GlobalDb);
                

                I can read a value from QML like this:

                //QML
                g_GlobalDatabase.gav_GlobalDb[VAR_INDEX]
                

                But I don't find how to write a value... I tried this but it doesn't work:

                //QML
                g_GlobalDatabase.gav_GlobalDb[VAR_INDEX] = true;
                

                Can you help me this that ?

                Thanks

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Delta_sim
                  wrote on last edited by
                  #8

                  @dheerendra @sierdzio any idea please ?

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    You're assigning bool to QList<QVariant>, no wonder it does not work. I do not know how to nicely fix this.

                    A hacky solution would be to add a Q_INVOKABLE method with the following signature:

                    Q_INVOKABLE void setGlobalDb(int index, const QVariant &value);
                    

                    Then from QML you could do:

                    g_GlobalDatabase.setGlobalDb(VAR_INDEX,  true)
                    

                    (Z(:^

                    1 Reply Last reply
                    1
                    • D Offline
                      D Offline
                      Delta_sim
                      wrote on last edited by
                      #10

                      Thanks @sierdzio it works with Q_INVOKABLE

                      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