From kde-bindings Wed Feb 17 15:25:32 2010 From: Arno Rehn Date: Wed, 17 Feb 2010 15:25:32 +0000 To: kde-bindings Subject: Re: [Kde-bindings] Qt, Qyoto buttons work intermediately Message-Id: <201002171625.32777.arno () arnorehn ! de> X-MARC-Message: https://marc.info/?l=kde-bindings&m=126642038125389 On Tuesday 16 February 2010 22:52:56 Linuxoid Oz wrote: > I've read that thing about the signals/slots and that was exactly what I > was trying to do (at least I thought that was exactly what they said). > Here's an example from a Qt Address Book tutorial: > > connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); > > As the QMessageBox needs a parent reference, 'this' is fine for the > MainWindow. This is my slot: > > QObject.Connect(btnAbout, Qt.SIGNAL("clicked()"), mainwin, > Qt.SLOT("ShowAbout(mainwin, page)")); > > First, the Qt one doesn't have these " ", so I thought it might be a Qyoto > thing. 'this' is a reference to MainWindow. In my StartWindow, 'mainwin' > is a reference to the MainWindow. If I use a slot in MainWindow I pass the > slot a 'this' reference as in the example. If I use a slot in the > StartWindow I pass it a reference to MainWindow as 'mainwin'. > > Are you saying if I declare the SecondWindow class as inherited from > :QWidget I should then pass the reference to itself as 'this' rather than > a reference to the MainWindow? > > In the StartWindow the QMessageBox also needs a parent reference, that's > why I pass 'mainwin' - reference to parent MainWindow. What do you mean > by passing a type rather than a variable? How do you pass a type if you > need to pass a reference to the parent window? If I pass anything else it > complains that that reference doesn't exist in the current context. OK, one last time: Objects can declare signals that they emit. And objects can also declare slots, which are special methods that are invoked upon a signal being emitted. For the meta object system to know which slot to invoke when a signal is emitted, you "connect" both. So when you do Connect(button, SIGNAL("clicked()"), otherObject, SLOT("buttonClicked()")); you tell the meta object system to invoke "otherObject.buttonClicked()" when the signal "clicked()" in the object "button" is emitted. Note that "otherObject" is _not_ a reference passed as a parameter, instead it's the RECEIVING OBJECT. It has to have a method (specifically, a SLOT), called "buttonClicked", which doesn't take any parameters. Upon the signal being emitted, this METHOD of the RECEIVING OBJECT is invoked. Now, if the signal declares parameters, you can of course connect to a slot that also takes parameters. Example: Connect(slider, SIGNAL("valueChanged(int)"), spinBox, SLOT("setValue(int)")); When the slider emits the signal "valueChanged", it carries additional information with it, in this case it's an integer containing the new value. The connection will cause the object "spinBox" to have its method "setValue" invoked, and the information that is carried by the signal (the integer) is directly passed to that method. So any change to the slider will cause the spinBox' display to be updated. Again: You connect a SIGNAL from a SENDING OBJECT to a SLOT of a RECEIVING OBJECT. For the connection to work properly, you have to give it the SIGNATURES of the SIGNAL and the SLOT. The third argument to Connect() is NOT a parent reference! It's the receiving object which has to have a method with the Q_SLOT attribute declared that matches the SIGNATURE in the SLOT() method! ------- To give you a bit more insight in the SIGNAL() and SLOT() methods: In C++ they are macros, which return the stuff that you give to them as a string with a prefixed number. SIGNAL( foo() ) - for example - will return "1foo()". Since C# doesn't have a preprocessor, the signature itself has to be a string already. The methods only add the number prefix. -- Arno Rehn arno@arnorehn.de _______________________________________________ Kde-bindings mailing list Kde-bindings@kde.org https://mail.kde.org/mailman/listinfo/kde-bindings