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. Use public slots or private slots?
QtWS25 Last Chance

Use public slots or private slots?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 9 Posters 14.2k Views
  • 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on 21 May 2017, 09:38 last edited by
    #1

    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?

    Isaac Pérez
    Programming is understanding.

    K V 2 Replies Last reply 21 May 2017, 10:24
    0
    • I isaacEnrique
      21 May 2017, 09:38

      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?

      K Offline
      K Offline
      koahnig
      wrote on 21 May 2017, 10:24 last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      7
      • I isaacEnrique
        21 May 2017, 09:38

        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?

        V Offline
        V Offline
        Venkatesh V
        wrote on 22 May 2017, 06:14 last edited by
        #3

        @isaacEnrique

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

        R 1 Reply Last reply 26 Sept 2023, 09:33
        3
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 22 May 2017, 06:48 last edited by
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          R 1 Reply Last reply 26 Sept 2023, 09:51
          5
          • J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 22 May 2017, 06:50 last edited by
            #5

            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.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • V Venkatesh V
              22 May 2017, 06:14

              @isaacEnrique

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

              R Offline
              R Offline
              Roobarb
              wrote on 26 Sept 2023, 09:33 last edited by Roobarb
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • D dheerendra
                22 May 2017, 06:48

                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.

                R Offline
                R Offline
                Roobarb
                wrote on 26 Sept 2023, 09:51 last edited by
                #7

                @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                S J S 3 Replies Last reply 26 Sept 2023, 09:58
                0
                • R Roobarb
                  26 Sept 2023, 09:51

                  @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                  S Offline
                  S Offline
                  sierdzio
                  Moderators
                  wrote on 26 Sept 2023, 09:58 last edited by
                  #8

                  @Roobarb said in Use public slots or private slots?:

                  @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                  You can use old connect syntax (SLOT macro), or invoke the slot via the QMetaObject, or call it via QML. All 3 will work even when a slot is private.

                  Lastly, you can make the connection inside the object which has the private slot :-) Then it is completely legal in C++.

                  (Z(:^

                  1 Reply Last reply
                  2
                  • R Roobarb
                    26 Sept 2023, 09:51

                    @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 Sept 2023, 09:59 last edited by JonB
                    #9

                    @Roobarb
                    Naturally this is the case, it's just standard C++.
                    As @Venkatesh-V wrote above

                    but if you want use those slot outside of the class then you should declare as a public slots.

                    If you choose private then naturally you can only connect() to it from within the class. If you want to connect() from elsewhere it must be public.

                    [ @sierdzio comments are also true, but suggest you do not go down that route, no need, use new style syntax and stick with connect(). ]

                    1 Reply Last reply
                    2
                    • R Roobarb
                      26 Sept 2023, 09:51

                      @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 26 Sept 2023, 17:22 last edited by
                      #10

                      @Roobarb said in Use public slots or private slots?:

                      @dheerendra But if you make the slot private, how can something outside of the slot class call connect() using it? When I try to use a private slot in my connect() call, the compiler says: error: 'onMessage' is a private member of 'my_slot'

                      Hi,

                      That's where you have to properly analyze whether having that slot public is a valid use case or not. Just making it public because it allows connection is not a good reason. Check your architecture first.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved