Use public slots or private slots?

  • I have some time working with Qt and I have always used private slots. I have never thought too much about it.

    Currently, I have an application with several dialog boxes, which in turn have a number of widgets. The functionalities that must be executed when the user interacts with such widgets
    are implemented as slots. Example: Clicking on a particular button zooms in on an image that is displayed.

    Now I ask myself:

    Is it correct that such slots are private?
    Would it be of any use to make such public slots?
    What is expected (standard) in these cases?

  • @isaacEnrique

    Well, slots are basically normal routines of your class, sicne you may use them also directly. Therefore, I would consider the private and public declaration as a matter of use and philosphy. If private is sufficient, I would use private slots. However, I am using slots across different classes and therefore, most of my slots are public.

  • @isaacEnrique

    there is no harm with using slot as a private member, if you are making use of those slots within the same class. it is just a normal member function there is nothing more than normal function... but if you want use those slot outside of the class then you should declare as a public slots.

  • Qt Champions 2017

    In addition previous posts, private or public slots have no significance with Qt C++ environment if you are using slots in the context of signal-to-slot communication. If you are interested to call this slots as normal member function then public/private is applicable. Also if you are trying to call the slots from QML environment, then slots need to be public only.

  • hi,
    if you decide to use mostly the new syntax for Signal/Slot connection, it breaks the problem even more down to "should this function be public or private".

    With the new syntax you actually do no longer need to specify a function as a SLOT, that part is now totaly ignored.
    It is ignored by the compiler anyway as it is only used by the Q_OBJECT macro.

    Howerver, it will make your header file much easier to read and understand, if you still do it.

Log in to reply

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