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?
Forum Updated to NodeBB v4.3 + New Features

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 828 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.
  • R Offline
    R Offline
    RobM
    wrote on 30 Sept 2020, 15:48 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 30 Sept 2020, 18:19 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 30 Sept 2020, 18:33
      1
      • S sierdzio
        30 Sept 2020, 18:19

        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 30 Sept 2020, 18:33 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?

        S 1 Reply Last reply 30 Sept 2020, 18:35
        0
        • R RobM
          30 Sept 2020, 18:33

          @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?

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 30 Sept 2020, 18:35 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

          3/4

          30 Sept 2020, 18:33

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved