Q_PROPERTY Question



  • 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



  • 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.



  • 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.



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



  • 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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.