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 do I access a Q_PROPERTY of a class from another class?
QtWS25 Last Chance

How do I access a Q_PROPERTY of a class from another class?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 731 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.
  • R Offline
    R Offline
    RobM
    wrote on last edited by RobM
    #1

    I have a Q_PROPERTY and I want to access the current state of that property from another class. Ie, this other class will make decisions based on the state. Here is the Q_PROPERTY:

    Q_PROPERTY(bool isCompact READ isCompact WRITE setIsCompact NOTIFY isCompactChanged)
    

    so what's the standard way of doing this? Do I need a getter for isCompact or do I create a new variable in the new class which will be updated whenever isCompactChanged or something else?

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

      this other class will make decisions based on the state

      Based on this alone, I'd say the proper way is to connect to isCompactChanged() in your other class. This way you don't even need to store a pointer to your class containing the property - you get a clear separation of data and responsibilities. It's easy to maintain and refactor such code.

      But if your use case is different, there is nothing wrong with calling the isCompact() method (getter) or even in some cases it's perfectly fine to call object->property("isCompact").toBool(). All depends on the gory details, as usual ;-)

      (Z(:^

      R 1 Reply Last reply
      1
      • sierdzioS sierdzio

        this other class will make decisions based on the state

        Based on this alone, I'd say the proper way is to connect to isCompactChanged() in your other class. This way you don't even need to store a pointer to your class containing the property - you get a clear separation of data and responsibilities. It's easy to maintain and refactor such code.

        But if your use case is different, there is nothing wrong with calling the isCompact() method (getter) or even in some cases it's perfectly fine to call object->property("isCompact").toBool(). All depends on the gory details, as usual ;-)

        R Offline
        R Offline
        RobM
        wrote on last edited by
        #3

        @sierdzio "...it's perfectly fine to call object->property("isCompact").toBool()"

        Interesting, what would this object be exactly? The Q_PROPERTY exists in a header GuidanceBarPresenter.h so would that be my object?

        sierdzioS 1 Reply Last reply
        0
        • R RobM

          @sierdzio "...it's perfectly fine to call object->property("isCompact").toBool()"

          Interesting, what would this object be exactly? The Q_PROPERTY exists in a header GuidanceBarPresenter.h so would that be my object?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @RobM said in How do I access a Q_PROPERTY of a class from another class?:

          @sierdzio "...it's perfectly fine to call object->property("isCompact").toBool()"

          Interesting, what would this object be exactly? The Q_PROPERTY exists in a header GuidanceBarPresenter.h so would that be my object?

          Yes, the object containing the property.

          (Z(:^

          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