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. Q_PROPERTY Question
Qt 6.11 is out! See what's new in the release blog

Q_PROPERTY Question

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.1k Views 1 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
    runslow
    wrote on last edited by
    #1

    Hello,

    I have a class in which I would like my Q_PROPERTY READ function to accept an argument. Here is my code:

    @
    class Simple : public QObject {
    Q_OBJECT
    Q_PROPERTY(QObject* apples READ getApples)

    public:
    QObject * getApples(int index);
    };
    @

    I always get a compiler error that says:

    no matching function for call to 'Simple::getApples()'
    QObject * Simple::getApples(int)
    note: candidate expects 1 argument, 0 provided

    So it seems that it is looking for a function without arguments. Is it possible to have a READ function take arguments?

    Thank you

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      The property mechanism assumes that there is a single underlying private member for each property. In your case, you have a property, whose getter does not refer to a single member. Properties are deeply integrated inside Qt and it may very well be that the getter should expect no arguments.

      You can still find a workaround for this - just go back to a no parameter getter and create an additional method, for example selectApples(int index) which sets the value of the QObject pointer to the desired one, before calling the getApples method.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        runslow
        wrote on last edited by
        #3

        Thank you for your reply. I do have a private variable _apples, but forgot to include it in here.

        I think I will create a public setIndex method.

        Thank you for your help.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tzander
          wrote on last edited by
          #4

          Maybe you want to instead have a property that is a QList<QObject*> where each of those is an apple ;)

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            Found this:

            bq. The READ function is const and returns the property type. The WRITE function returns void and has exactly one parameter of the property type. The meta-object compiler enforces these requirements.

            Although the WRITE function does seem to work OK with return other than void...

            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