Qt-interest Archive, January 2007
Public slots or private slots
Pages: Prev | 1 | 2 | Next
Message 1 in thread
Hi Everyone,
Would anybody tell me what is the difference between "public slots"
declaration
and "private slots" declaration ??
Regards,
Pagan
--
[ signature omitted ]
Message 2 in thread
On 1/16/07, Pagan Chou <pagan_chou@xxxxxxxxxxxxxx> wrote:
> Hi Everyone,
> Would anybody tell me what is the difference between "public slots"
> declaration
> and "private slots" declaration ??
Exactly the same, as between regular public and private methods in
C++. Slots are just normal class methods with exception of their
ability to be connected to signals.
--
[ signature omitted ]
Message 3 in thread
On Tuesday 16. January 2007 10:05, Pavel Antokolsky aka Zigmar wrote:
> On 1/16/07, Pagan Chou <pagan_chou@xxxxxxxxxxxxxx> wrote:
> > Hi Everyone,
> > Would anybody tell me what is the difference between "public slots"
> > declaration
> > and "private slots" declaration ??
>
> Exactly the same, as between regular public and private methods in
> C++. Slots are just normal class methods with exception of their
> ability to be connected to signals.
But when connecting to signals the private/public declaration is circumvented
since the connection is not evaluated at compile time. It is resolved at
runtime with a string signature of the slot and this does not contain any
information about private/public-ness
--
[ signature omitted ]
Message 4 in thread
Tuesday 16 January 2007 09:56 skrev Pagan Chou:
> Hi Everyone,
> Would anybody tell me what is the difference between "public slots"
> declaration
> and "private slots" declaration ??
Exactly the same as public and private methods - you can't call private slots
from other classes.
Bo.
--
[ signature omitted ]
Message 5 in thread
Pagan Chou schrieb:
> Hi Everyone,
> Would anybody tell me what is the difference between "public slots"
> declaration
> and "private slots" declaration ??
Yes, the former is "public", the later is "private".
Any more questions?
:)
No seriously: it's the same as with normal method declarations: "public"
and "private" apply to the accessibility, e.g. "public" means "you can
call the method from anywhere else", and "private" means you can call
the method (or slots) only from the same class (not even from a
subclass; that's where "protected" comes into play).
But this is rather a very basic OO question, and I'm only answering it
because I'm such a nice guy ;) For details refer to any good book about
OO Programming or google a bit. Wikipedia is also very helpful.
Cheers, Oliver
--
[ signature omitted ]
Message 6 in thread
Thanks a lot for all you guys' response.
But I don't understand why I still can connect to a private slots
from outside of this class ??
"Till Oliver Knoll" <oliver.knoll@xxxxxxxxxxx> ???
news:45AC99D4.8060601@xxxxxxxxxxx ???...
> Pagan Chou schrieb:
> > Hi Everyone,
> > Would anybody tell me what is the difference between "public slots"
> > declaration
> > and "private slots" declaration ??
>
> Yes, the former is "public", the later is "private".
>
> Any more questions?
>
> :)
>
> No seriously: it's the same as with normal method declarations: "public"
> and "private" apply to the accessibility, e.g. "public" means "you can
> call the method from anywhere else", and "private" means you can call
> the method (or slots) only from the same class (not even from a
> subclass; that's where "protected" comes into play).
>
> But this is rather a very basic OO question, and I'm only answering it
> because I'm such a nice guy ;) For details refer to any good book about
> OO Programming or google a bit. Wikipedia is also very helpful.
>
> Cheers, Oliver
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
"unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 7 in thread
Pagan Chou schrieb:
> Thanks a lot for all you guys' response.
>
> But I don't understand why I still can connect to a private slots
> from outside of this class ??
Indeed, I beg to differ with the guru's answers above: Private slots can
be connected to and called by signals from other classes. However, you
can't connect to them from outside of the class that declares them,
because they aren't visible there - which means the connect() call for
it has to occur from inside the class with the private slot.
Well, that's my understanding of it, at least...
Martin
--
[ signature omitted ]
Message 8 in thread
Martin,
I don't quite understand what you mean.
You mean I can't connect to _InternalSlots from outside of class A ??
But now I can and it works fine. That's why it bothers me.
class A
{
Q_OBJECT
private slots:
void _InternalSlots(int);
};
"Martin Gebert" <Martin.Gebert@xxxxxxxxxxxxxxxxxxxxxxxxxxx> ???
news:eoi94t$rjg$1@xxxxxxxxxxxxxxxxxx ???...
> Pagan Chou schrieb:
> > Thanks a lot for all you guys' response.
> >
> > But I don't understand why I still can connect to a private slots
> > from outside of this class ??
>
> Indeed, I beg to differ with the guru's answers above: Private slots can
> be connected to and called by signals from other classes. However, you
> can't connect to them from outside of the class that declares them,
> because they aren't visible there - which means the connect() call for
> it has to occur from inside the class with the private slot.
> Well, that's my understanding of it, at least...
>
> Martin
>
> --
> To mail me in private, please remove the -NOSPAM- part from the adress
> above.
>
> Ultimate Truths in software development # 3:
> PEBKAC
--
[ signature omitted ]
Message 9 in thread
Pagan Chou wrote:
> Martin,
> I don't quite understand what you mean.
>
> You mean I can't connect to _InternalSlots from outside of class A ??
> But now I can and it works fine. That's why it bothers me.
>
> class A
> {
> Q_OBJECT
>
> private slots:
> void _InternalSlots(int);
> };
>
Just to make sure: you mean you can do this?
// another class B, tries to connect to private slot of class A
class B : public QObject
{
Q_OBJECT
public:
...
void frenchConnection()
{
A *a;
...
// are you saying that this works? Also at runtime (is the slot
// actually called then)?
this->connect (this, SIGNAL (foo(int)),
a, SLOT (_InternalSlots(int)));
}
...
}
To be honest I have actually never tried this myself, but I would at
least expect a runtime error saying "No such slot" (not sure if the SLOT
macro itself is clever enough at compile time as to see that the slot is
declared private), no?
Anyone else actually tried this?
Cheers, Oliver
--
[ signature omitted ]
Message 10 in thread
C++ prohibits this:
A::myFunction()
{
B *b = new B();
b->privateSlot();
}
but Qt allows this:
A::myFunction()
{
B *b = new B();
connect(this, SIGNAL(mySignal()), b,
SLOT(privateSlot()));
}
....which, in a sense, breaks encapsulation (but let's not go there :-).
Sam Dutton
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E SAM.DUTTON@xxxxxxxxx
WWW.ITN.CO.UK
-----Original Message-----
From: Martin Gebert [mailto:Martin.Gebert@xxxxxxxxxxxxxxxxxxxxxxxxxxx]
Sent: Tuesday 16 January 2007 10:27
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: Public slots or private slots
Pagan Chou schrieb:
> Thanks a lot for all you guys' response.
>
> But I don't understand why I still can connect to a private slots from
> outside of this class ??
Indeed, I beg to differ with the guru's answers above: Private slots can
be connected to and called by signals from other classes. However, you
can't connect to them from outside of the class that declares them,
because they aren't visible there - which means the connect() call for
it has to occur from inside the class with the private slot.
Well, that's my understanding of it, at least...
Martin
--
[ signature omitted ]
Message 11 in thread
>> Anyone else actually tried this? <<
I just tried this (see my other post) and yes, connection to a private
slot in another class is allowed at compile time (in Visual Studio, at
least) and the slot is called at run time.
You could argue that the private slot is called from an object of its
class but, in practice, this is a way of getting around encapsulation.
Sam
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E SAM.DUTTON@xxxxxxxxx
WWW.ITN.CO.UK
-----Original Message-----
From: Till Oliver Knoll [mailto:oliver.knoll@xxxxxxxxxxx]
Sent: Tuesday 16 January 2007 11:35
To: Qt Interest List
Subject: Re: Public slots or private slots
Pagan Chou wrote:
> Martin,
> I don't quite understand what you mean.
>
> You mean I can't connect to _InternalSlots from outside of class A ??
> But now I can and it works fine. That's why it bothers me.
>
> class A
> {
> Q_OBJECT
>
> private slots:
> void _InternalSlots(int);
> };
>
Just to make sure: you mean you can do this?
// another class B, tries to connect to private slot of class A class B
: public QObject {
Q_OBJECT
public:
...
void frenchConnection()
{
A *a;
...
// are you saying that this works? Also at runtime (is the slot
// actually called then)?
this->connect (this, SIGNAL (foo(int)),
a, SLOT (_InternalSlots(int)));
}
...
}
To be honest I have actually never tried this myself, but I would at
least expect a runtime error saying "No such slot" (not sure if the SLOT
macro itself is clever enough at compile time as to see that the slot is
declared private), no?
Anyone else actually tried this?
Cheers, Oliver
--
[ signature omitted ]
Message 12 in thread
Tuesday 16 January 2007 13:00 skrev Dutton, Sam:
> >> Anyone else actually tried this? <<
>
> I just tried this (see my other post) and yes, connection to a private
> slot in another class is allowed at compile time (in Visual Studio, at
> least) and the slot is called at run time.
>
> You could argue that the private slot is called from an object of its
> class but, in practice, this is a way of getting around encapsulation.
It is always allowed. A connect means it will call the dispatcher
qt_metacall() which is a public method. And this is the one that does the
call to a slot. So with a connected slot, it doesn't matter if it's public,
protected, or private.
To get around this, the trolls would need three dispatchers
qt_metacall_(public, protected, private) and call the private when inside the
class code. That's not easy to do.
Bo.
> -----Original Message-----
>
> From: Till Oliver Knoll [mailto:oliver.knoll@xxxxxxxxxxx]
> Sent: Tuesday 16 January 2007 11:35
> To: Qt Interest List
> Subject: Re: Public slots or private slots
>
> Pagan Chou wrote:
> > Martin,
> > I don't quite understand what you mean.
> >
> > You mean I can't connect to _InternalSlots from outside of class A ??
> > But now I can and it works fine. That's why it bothers me.
> >
> > class A
> > {
> > Q_OBJECT
> >
> > private slots:
> > void _InternalSlots(int);
> > };
>
> Just to make sure: you mean you can do this?
>
> // another class B, tries to connect to private slot of class A class B
>
> : public QObject {
>
> Q_OBJECT
>
> public:
> ...
> void frenchConnection()
> {
> A *a;
> ...
> // are you saying that this works? Also at runtime (is the slot
> // actually called then)?
> this->connect (this, SIGNAL (foo(int)),
> a, SLOT (_InternalSlots(int)));
> }
> ...
> }
>
> To be honest I have actually never tried this myself, but I would at
> least expect a runtime error saying "No such slot" (not sure if the SLOT
> macro itself is clever enough at compile time as to see that the slot is
> declared private), no?
>
> Anyone else actually tried this?
>
> Cheers, Oliver
>
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> Please Note:
>
>
>
> Any views or opinions are solely those of the author and do not necessarily
> represent those of Independent Television News Limited unless specifically
> stated. This email and any files attached are confidential and intended
> solely for the use of the individual or entity to which they are addressed.
> If you have received this email in error, please notify
> postmaster@xxxxxxxxx
>
> Please note that to ensure regulatory compliance and for the protection of
> our clients and business, we may monitor and read messages sent to and from
> our systems.
>
> Thank You.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 13 in thread
Bo Thorsen wrote:
> Tuesday 16 January 2007 13:00 skrev Dutton, Sam:
>
>>>>Anyone else actually tried this? <<
>>
>>I just tried this (see my other post) and yes, connection to a private
>>slot in another class is allowed at compile time (in Visual Studio, at
>>least) and the slot is called at run time.
>>
>>You could argue that the private slot is called from an object of its
>>class but, in practice, this is a way of getting around encapsulation.
>
>
> It is always allowed. A connect means it will call the dispatcher
> qt_metacall() which is a public method. And this is the one that does the
> call to a slot. So with a connected slot, it doesn't matter if it's public,
> protected, or private.
Huh... I wasn't aware of this, but now that you mention it this makes
completely sense. As to summarize what this means in practice for slots:
- The public/protected/private only applies when the slot is to be
called directly (as a normal method)
- For connecting signals with slots the public/protected/private
modifiers are completely meaningless: you can connect a signal
from within any class to a slot in any (other) class
Still I would regard this as "bad coding style" to connect a private
slot from any other class than the actual class where this private slot
is implemented in.
Cheers, Oliver
--
[ signature omitted ]
Message 14 in thread
Tuesday 16 January 2007 14:28 skrev Till Oliver Knoll:
> [...]
> Still I would regard this as "bad coding style" to connect a private
> slot from any other class than the actual class where this private slot
> is implemented in.
Yes, but it allows you to sometimes do some amazingly evil hacks on the Qt
classes :-)
Bo.
--
[ signature omitted ]
Message 15 in thread
Bo Thorsen wrote:
> Tuesday 16 January 2007 14:28 skrev Till Oliver Knoll:
>
>>[...]
>>Still I would regard this as "bad coding style" to connect a private
>>slot from any other class than the actual class where this private slot
>>is implemented in.
>
>
> Yes, but it allows you to sometimes do some amazingly evil hacks on the Qt
> classes :-)
Indeed! You need to call this very private method in that Qt class which
also happens to be a slot? And your stupid compiler prohibits you in
doing so, brabbling something like "private method call"?
No problem! Here's the plan for evil programmers >:b
- Create a signal in your class "evilSignal"(tm)
- Connect the evilSignal(tm) to the private slot of your choice.
- Need to call this private slot now? Simply 'emit evilSignal()'!
It's as easy as that ;)
Cheers, Oliver
--
[ signature omitted ]
Pages: Prev | 1 | 2 | Next