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. Why do Signals have to be public?

Why do Signals have to be public?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 910 Views 1 Watching
  • 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.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on last edited by
    #1

    Ever since I started using the signals-slots mechanism I found it incredibly easy to communicate between objects. However, here are a few questions that I have about this mechanism:

    1- Why do Signals have to be public? (Please explain the underlying mechanism)
    2- Do slots need to be public as well? (if yes, then why? and if not, then which is better public/private?)

    Christian EhrlicherC 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      By emitting a signal object announces change in its state. It doesn't care if anyone is listening and it shouldn't, as it would couple it to that receiver in some way. Private signal would be a mix of concerns. The object is supposed to announce the change. It's not supposed to filter/manage who's able to get the notification. It's the job of the user to make the right connections.

      That's the design reason. A technical one is that in Qt4 you made connections only via SIGNAL macro, which just normalized signal name as a string, so signals could be private. With the function pointer based syntax you wouldn't be able to specify private &Class::signalName in the static QObject::connect.

      Slots are a different story. In Qt5 and 6 you can make a connection to any functor, not just slots. For the purpose of connections slots are pretty much just regular member functions, so they can be public, private or protected like any other method. The only difference is they are registered as slots in meta object and can be used in the old string based connections.

      As to which is better - public or private. There's no better or worse. It depends on a use case. Like with regular methods you, as a designer of the class, decide what is the desired access level for given method.

      1 Reply Last reply
      7
      • SavizS Saviz

        Ever since I started using the signals-slots mechanism I found it incredibly easy to communicate between objects. However, here are a few questions that I have about this mechanism:

        1- Why do Signals have to be public? (Please explain the underlying mechanism)
        2- Do slots need to be public as well? (if yes, then why? and if not, then which is better public/private?)

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Saviz said in Why do Signals have to be public?:

        1- Why do Signals have to be public? (Please explain the underlying mechanism)
        2- Do slots need to be public as well? (if yes, then why? and if not, then which is better public/private?)

        Treat them as normal functions and you get the answer by yourself.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        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