Results 1 to 13 of 13

Thread: External Acces to Private Slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: External Acces to Private Slot

    Maybe you were confused by my response, as it basically states exactly the same as what you just replied with. Eg. that obj->func() will not work from outside the class but obj->InvokeMethod will.

    To have your slots truly private, create them in another private class, and contain a pointer to this class in the private part of your public class. So if you need to access your private slots you can do d->slot() whilst people outside your class can not, as 'd' would be private.

    This also means your class is more flexible - you can either have data for each instance of your class, or you can share data between instances of your class (like the Qt object classes).

  2. The following 3 users say thank you to squidge for this useful post:

    borisbn (20th July 2010), stefanadelbert (19th July 2010), Zlatomir (19th July 2010)

  3. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: External Acces to Private Slot

    Slots, by their nature, are public, period. Private slots make no sense; private members are not accessible outside the class they belong to, so signals and slots make no sense, since a direct function call will do just as well in such instances. Slots are inherently public; they are meant explicitly for inter-class communication.

    The whole pedantic discussion above would make more sense if protected slots were the topic. For private slots, it's just silly. And the protected case is actually more interesting; here, in order to preserve the traditional C++ meaning of "protected", the metadata system would have to keep track of object inheritance to ensure that only related classes could signal one another - which, again, is somewhat silly since the whole point is inter-object communication, but here it's somewhat less silly than it is when the topic is private slots. In the end, however, one would have to reproduce a signficant chunk of the C++ compiler in order to pull this sort of thing off.

    And for what purpose? The goto statement is also considered "bad", but lots of people still use it, and even modern languages like Java support it despite being able to do perfectly well without it through the use of other, "better" constructs. Varying degrees of access protection are useful, but insisting on strict adherence to this paradigm in a signalling system is pointless. Anyone concerned about this is, presumably, a programmer. By implication, that means you should be aware of the potential pitfalls, and should simply avoid them.

  4. The following 2 users say thank you to SixDegrees for this useful post:

    stefanadelbert (19th July 2010), Zlatomir (19th July 2010)

  5. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default

    Quote Originally Posted by fatjuicymole View Post
    Maybe you were confused by my response, as it basically states exactly the same as what you just replied with. Eg. that obj->func() will not work from outside the class but obj->InvokeMethod will.

    To have your slots truly private, create them in another private class, and contain a pointer to this class in the private part of your public class. So if you need to access your private slots you can do d->slot() whilst people outside your class can not, as 'd' would be private.

    This also means your class is more flexible - you can either have data for each instance of your class, or you can share data between instances of your class (like the Qt object classes).
    Nice suggestion. Thanks.

    Quote Originally Posted by ahmdsd_ostora View Post
    I think that Qt should prevent connecting to private slots outside their classes.
    This is basically what I'm saying. I don't believe that it should be possible for a private slot to be accessible outside that defining class.

    Quote Originally Posted by SixDegrees View Post
    Slots, by their nature, are public, period. Private slots make no sense; private members are not accessible outside the class they belong to, so signals and slots make no sense, since a direct function call will do just as well in such instances. Slots are inherently public; they are meant explicitly for inter-class communication.

    The whole pedantic discussion above would make more sense if protected slots were the topic. For private slots, it's just silly. And the protected case is actually more interesting; here, in order to preserve the traditional C++ meaning of "protected", the metadata system would have to keep track of object inheritance to ensure that only related classes could signal one another - which, again, is somewhat silly since the whole point is inter-object communication, but here it's somewhat less silly than it is when the topic is private slots. In the end, however, one would have to reproduce a signficant chunk of the C++ compiler in order to pull this sort of thing off.

    And for what purpose? The goto statement is also considered "bad", but lots of people still use it, and even modern languages like Java support it despite being able to do perfectly well without it through the use of other, "better" constructs. Varying degrees of access protection are useful, but insisting on strict adherence to this paradigm in a signalling system is pointless. Anyone concerned about this is, presumably, a programmer. By implication, that means you should be aware of the potential pitfalls, and should simply avoid them.
    Thanks for your (fueled) response and your good points. Let's not get started on the inheritance of signals and slots though. It's clearly something that the Qt Keepers are aware of and it might be fixed up down the line.

    I take your point about Qt signals and slots being an inter-class communication mechanism, but it is also very useful in other situations. For example,

    Class A has a QTimer. It wants to do something private (modify some or other private member) when the timer times out. It connects signal QTimer::timeout() to its (private) slot MyVeryPrivateSlot(). But now that private slot can be connected to and triggered by anyone outside the class. Sure class A could call private function MyVeryPrivateFunction(), but how else could class A respond to QTimer::timeout() other than with a slot?

    It was not my intention to enter into a ridiculously pedantic debate on the intricacies of Qt's signalling framework. I'm a fan, else I wouldn't be here. Just wanted to get some opinions on the topic and find out if there were any creative solutions.
    Last edited by stefanadelbert; 20th July 2010 at 00:27.

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: External Acces to Private Slot

    There is a nice button on this forum under every post called "Multi-Quote This Message". Please use it next time instead of posting multiple posts.

    As for the discussion: remember that access protection is done purely during compilation based on a very simple rule, there is no actual check made (run-time) whether one can call a particular method or not hence enforcing such check during run-time for Qt slots would only create overhead without any practical benefits. Many languages (like Python) have simply ignored access protection and treat all methods and fields as public. Working around protection in C++ requires 4-5 lines of code so if someone wants to have access to a private or protected member of some class, there is practically no way of stopping him. On pure academic level I can understand that Qt should support access protection to its signals and slots but as an engineer I say there is no practical argument to support this claim.

    but how else could class A respond to QTimer::timeout() other than with a slot?
    Through events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    stefanadelbert (20th July 2010)

Similar Threads

  1. Timer not connecting to private SLOT
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 16th December 2009, 00:07
  2. Acces parent object
    By Nightfox in forum Qt Programming
    Replies: 8
    Last Post: 19th August 2009, 08:56
  3. Acces a stacked widget...?
    By ucomesdag in forum Qt Programming
    Replies: 5
    Last Post: 25th November 2006, 14:05
  4. Acces to a remote mysql server
    By Alienxs in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2006, 03:10
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.