Results 1 to 13 of 13

Thread: External Acces to Private Slot

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default

    You all make valid points. I'm definitely not criticizing Qt's SIGNAL / SLOT mechanism.

    My observation is merely that class A would be able to execute a private slot on class B. There is no way that class B can prevent its private slot (which is actually just a private function that is registered as part of the associated metaObject) from being run by any other SIGNAL / SLOT aware class.

    Say for instance that I want to create a private slot on class B that should only be run by class B:

    Qt Code:
    1. class B : public QObject
    2. {
    3. private slots:
    4. void MyVeryPrivatePersonalSlot(); // I, class B, should be the only class able to run the code in this function
    5. }
    To copy to clipboard, switch view to plain text mode 

    But the private slot can be "called" (or triggered) by any class that can emit signals. So really the slot is not private at all.

    Qt Code:
    1. class A : public QObject
    2. {
    3. A() {
    4. instanceOfB = new B();
    5. connect(this, SIGNAL(mySignal()), instanceOfB, SLOT(MyVeryPrivatePersonalSlot()));
    6. }
    7.  
    8. signals:
    9. void mySignal();
    10.  
    11. private:
    12. B* instanceOfB;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Is there any way to prevent this behaviour, i.e. keep private slots truly private?

    Signal-Slot pattern aim is that the sender DOESN'T KNOW which slots will be called via its signal.
    so actually the sender doesn't call the slot, he just emits the signal and the environment deals with it.
    I agree with you and this is all well and good when slot bar() is public. But when slot bar() is private, class B has no control over who can trigger the execution of the private slot's code.

    Quote Originally Posted by ahmdsd_ostora View Post
    Signal-Slot pattern aim is that the sender DOESN'T KNOW which slots will be called via its signal.
    so actually the sender doesn't call the slot, he just emits the signal and the environment deals with it.
    But in the case when class A, the sender (signaller), connect's its signal to class B's private slot (as in my example), then it does know exactly which function should be executed and it can choose to have code in private functions executed. In C++ this can be achieved using friends, but still class B would decide who its friends are so it still have the control.

    I understand that the code isn't being called directly - it's being done through the metaObject framework, but the end effect is that a private function is exposed.

    Quote Originally Posted by borisbn View Post
    slots are not a usual functions. they are a part of metaObject. when you emitting signal slot is not called directly like this
    Qt Code:
    1. obj->slotName( params );
    To copy to clipboard, switch view to plain text mode 
    emitting signal say to object to call it's function like this:
    Qt Code:
    1. obj->invokeMethod( "slotName" // you see - in quotes
    2. , params );
    To copy to clipboard, switch view to plain text mode 
    I agree and it's useful framework and I do understand (more or less) how the framework actually works. My concern is that private slots are available to anyone and everyone, and this breaks part of the C++ class member access pattern.

    Quote Originally Posted by fatjuicymole View Post
    You can't call the slot directly, but you can call it via the slot mechanism as those are resolved during run time.
    I disagree, but I'm very happy to be proved wrong.

    A slot is nothing other than a function on a class and can be called the same way that any other function on a class would be called. But it is also considered when building the metaObject and is made available through the signal / slot mechanism. When the slot is called as a normal function the access specifier (public, protected, private) is respected. When the slot is triggered by a signal (whether the class' own signal or a signal from another class), the access specifier is not respected, resulting in private functions (slots) being runnable (triggerable) from outside the class.

    The net effect is that private slots can be run by anyone.
    Last edited by wysota; 20th July 2010 at 00:03.

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.