The OP wants to avoid the manual boilerplate of getter/setter pairs that would not need to be written if we didn't want to expose a Q_PROPERTY.
I don't have a solution for that, but I am still interested in the "private members" aspect of the question.
In my case, I arrived here because I wanted to hide these required setters from all other code EXCEPT for Qt binding code.
Empirically, using Qt 5.12, the following does work for me:
class HolderOfSomeInteger : public QObject {
Q_OBJECT
Q_PROPERTY(int someInt
READ GetInt
NOTIFY someIntChanged)
signals:
void someIntChanged();
private: // <--- private section
// My own other classes cannot access this, but
// the QML binding works as expected anyhow.
int GetInt() const { return some_integer; }
int some_integer = 0;
};
So, in addition to keeping the int
data-member some_integer
private, I apparently can simply put the GetInt()
getter in the private
section as well.
However, as @adrian-maire said in https://stackoverflow.com/a/42348046/10278, "for some versions of Qt, you may find some tricks working, but they may stop working in a following version."
As this Qt Property System documentation looks today, it only says
"A READ accessor function is required if no MEMBER variable was
specified. It is for reading the property value. Ideally, a const
function is used for this purpose, and it must return either the
property's type or a const reference to that type."
It says nothing about whether the accessor function must be public or private.
I dug some more, and I think I found out why the property binding still works with a private getter.
The Q_OBJECT
macro declares that your class has methods qt_metacall
and qt_static_metacall
. Qt's MOC
then generates the bodies of these methods.
The property binding is accomplished using those methods. Since those methods are members of your class, they (of course) can call even the private
member functions that your class provides.