Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. It is possible to have a read only Q_PROPERTY with MEMBER?
QtWS25 Last Chance

It is possible to have a read only Q_PROPERTY with MEMBER?

Scheduled Pinned Locked Moved Solved General and Desktop
qpropertyreadonly
5 Posts 2 Posters 8.1k 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
    mcleary
    wrote on last edited by
    #1

    I build a system that automatically create interface elements for editing properties declared with Q_PROPERTY.
    I'm using QMetaProperty::isWritable() method to enable or disable a widget that will be used to edit the corresponding property.
    I'm also using MEMBER feature to keep my member variables with a m_ prefix.
    The problem is that, as stated in the documentation:

    A MEMBER variable association is required if no READ accessor function is specified. This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. It's still possible to use READ or WRITE accessor functions in addition to MEMBER variable association (but not both), if you need to control the variable access.

    apparently there is no way to create a read-only property using the MEMBER feature. So I would like to confirm if I can have a property with this characteristic.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcleary
      wrote on last edited by
      #2

      I just realized that I can declare a property like this:

      Q_PROPERTY(QString name READ name)

      and I do not necessarily need to have a member in my QObject to store the property.

      But I still don't know the implications of doing so.

      1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @mcleary
        What about:

        Q_PROPERTY(QString name MEMBER m_name READ name)
        

        This should generate your variable and a getter, but without the setter.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcleary
          wrote on last edited by
          #4

          @kshegunov the problem I was facing is that the QMetaProperty was reporting that the property is writable even if I don't specify a setter function.

          If you want to check, you can use this code:
          Where m is some QObject with Q_PROPERTIES

          auto object = &m;
          const QMetaObject *metaobject = object->metaObject();
          int count = metaobject->propertyCount();
          for (int i=0; i<count; ++i) {
              QMetaProperty metaproperty = metaobject->property(i);
              const char *name = metaproperty.name();
              QVariant value = object->property(name);
          
              qDebug() << name <<
                          value <<
                          "NOTIFY" << metaproperty.hasNotifySignal() <<
                          "WRITABLE" << metaproperty.isWritable();
          
              if(!metaproperty.isWritable())
              {
                  qDebug() << name << "READONLY" << value;
              }        
          }
          

          When one declares a property with MEMBER feature the QMetaProperty is reporting that the property is writable.
          I solve my problem removing the MEMBER declaration!

          kshegunovK 1 Reply Last reply
          0
          • M mcleary

            @kshegunov the problem I was facing is that the QMetaProperty was reporting that the property is writable even if I don't specify a setter function.

            If you want to check, you can use this code:
            Where m is some QObject with Q_PROPERTIES

            auto object = &m;
            const QMetaObject *metaobject = object->metaObject();
            int count = metaobject->propertyCount();
            for (int i=0; i<count; ++i) {
                QMetaProperty metaproperty = metaobject->property(i);
                const char *name = metaproperty.name();
                QVariant value = object->property(name);
            
                qDebug() << name <<
                            value <<
                            "NOTIFY" << metaproperty.hasNotifySignal() <<
                            "WRITABLE" << metaproperty.isWritable();
            
                if(!metaproperty.isWritable())
                {
                    qDebug() << name << "READONLY" << value;
                }        
            }
            

            When one declares a property with MEMBER feature the QMetaProperty is reporting that the property is writable.
            I solve my problem removing the MEMBER declaration!

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @mcleary

            When one declares a property with MEMBER feature the QMetaProperty is reporting that the property is writable.

            This is quite strange indeed.

            I solve my problem removing the MEMBER declaration!

            This is certainly a possibility. Still I'm glad it's working for you,

            Read and abide by the Qt Code of Conduct

            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