[Kde-bindings] Qt, Qyoto buttons work intermediately

IBBoard ibboard at gmail.com
Thu Feb 18 16:07:40 UTC 2010

On 18/02/10 09:44, linuxoidoz at yahoo.com.au wrote:
> So if I understand it right, if I have one class MainWindow with a button btn1, and another class StartWindow with a button btn2, then you use this
> Connect(button, SIGNAL("clicked()"), otherObject.btn2, SLOT("buttonClicked()"));
> to emit a signal buttonClicked() from btn2 in StartWindow when button btn1 in MainWindow has been clicked? 

Not quite. That would tell the system that the slot ("target function"
or "delegate") "buttonClicked()" on "otherObject.btn2" should be
executed when the "clicked()" signal ("event", in effect) occurs.

>> you tell the meta object system to invoke "otherObject.buttonClicked()" when the signal "clicked()" in the object "button" is emitted
> In C# you declare a method first as
> this.btn1.Clicked += new System.EventHandler(this.OnBtn1Clicked);
> and then implement it as
> protected virtual void OnBtn1Clicked (object sender, System.EventArgs e)
> How do you do this with Qt in C#? Sorry, I just can't understand this signal/slot thing without comparable examples. All I need to know is, for example, how to show a message box from another class and pass a variable to it.

I've got a worked example using delegates that I mentioned before. I've
actually separated it out in to a helper function (because I _hate_ the
magic strings and want to type them as little as possible), but the full
code is available online at

Basically, if "menuAction" is a QAction (e.g. from a menu) and "method"
is a method that matches the SlotFunction delegate signature then:

QObject.Connect(menuAction, SIGNAL("triggered()"), method);

will add the delegate as a slot method that gets called when the
triggered signal (clicking the menu item) occurs.

> This
> 		QObject.Connect(btnAbout, Qt.SIGNAL("clicked()"), this, Qt.SLOT("ShowAbout(variable)"));
> 		[Q_SLOT]
> 		private void ShowAbout() {
> 			QMessageBox.About(this, "About", variable.ToString());
> 		}
> simply doesn't work. How can you pass a reference to a widget in the SLOT if you're saying you need to pass a type, NOT a variable, when the variable is invisible to the QMessageBox unless you pass it to the method.

I've not tried it with variables, and I don't think it works because it
occurs in a similar way to delegates and events - i.e. it'll be the
clicked() signal that is passing the variable (in this case, none) and
you have no control over what that passes at the time of registering the

> If you could send me a link to signal/slot C# examples, I'm happy to read, but this Qt-C# signal/slot theory is too different to plain C# to comprehend.

I agree that it is less than pretty and not ideal/'natural' in the .Net
environment, but that's the way that Qt does it. It also provides more
functionality than events/delegates in terms of overloading. I'm not
sure that I think overloading signals is a good idea in general
(basically having multiple events with the same name but different
signatures and picking the appropriate) but that's the way that Qt does
it and so Qyoto has to support it.

As for Richard's comment - slots and signals are straightforward *if*
you know all of your slot strings. If you don't then there's quite a
learning curve and a lot of looking things up, especially since you
don't find out that you did it wrong until it tries to execute that line
and it prints a warning.

On the plus side, at least you don't have to cope with all the iterators
and C-like API of GTK# ;)

More information about the Kde-bindings mailing list