Search code examples
qtqmlpublicprivate-members

Make a private C++ function visible to QML


I have been making my C++ functions callable from Qml by following the approach given in Qt documentation.

This requires one of these conditions to be fulfilled :

  1. Make the C++ function public and Q_INVOKABLE or
  2. Make the C++ function a public slot

This is sometimes not in sync with my class design. As in, the function which I want to be callable from QML, is private according to my class design.

Q1. Can I make a function visible to QML and still keep it private?

Q2. Is it my design flaw to expect this kind of behavior?


Solution

  • If you make something private by design, you consider that it's something to be used only within a class.

    What you are asking actually is how can I work around my design.

    Obvious answer is — you can make a public wrapper in a class which will invoke your private method and publish this public wrapper into QML, but I would suggest to review design if you face such situation.