views:

140

answers:

4

Is there any way to use the signals without MOC and without the connecting via names? My one problem with Qt is that you have something like

this->connect(this->SaveBtn, SIGNAL(click()), SLOT(SaveClicked()));

And there is no error detection to tell that is wrong other then finding out the button doesn't work or searching through their documentation to find out the signal doesn't exist. Also it seems pointless and a waste of cycles to connect via names instead of classes.

A: 

Other than the fact that signals are just methods, no I don't think you can use them like they are intended without the intermediate MOC step. It is a pain when you mess up the connection and there is no flag raised. What does your last sentence mean? Can you elaborate what your problem is? Signals/Slots is not a perfect system, I don't think a perfect system exits, but it is pretty intuitive and has worked well for me.

JimDaniel
Well why connect with names when you can have an array of callbacks?
Even with an array of callbacks, I can't even think how it would work without having to specify at some point what needs connecting to what, using names. How do you think it should work specifically?
JimDaniel
Well personally I would like a gui library that does it like .net with events. I would just use .net but I don't want to limit my project to needing the .net framework installed. Especially just for a GUI.
You don't specify names when hooking up .net events? Maybe I'm confused what you mean by names.
JimDaniel
Well for .net you do. "Event += new EventHandler(Function);"Then to call all the handlers you do. "Event(Args);"
In this sense, the Signal/Slot method is similar to .Net. The connect() syntax takes the place of "Event += new EventHandler(Function);" and "emit mySignal(args);" takes the place of "Event(Args);" At the end of the day it's just two different implementations of the Observer pattern - the .Net implementation just hides more of the plumbing from you.
JimDaniel
Ya, but Qt does it by forcing you to MOC the class and connects via names.
:-) You're just gonna have to deal with it, or roll your own event system if you think you can do better. Like I said, .Net does something similar to the MOC process in the background, because you have to register/unregister the callbacks - it's just that in Qt the automation part is more transparent.
JimDaniel
MOC is something entirely different. It is what adds the stupid "public slots:" and such to the classes and generates the real code.
No, it is the same concept, different implementation. Do you think the "event" and "delegate" keywords in C# are something magical? The CSC parses those just like the MOC parses "public slots:". The only real difference is that the developers of Qt must build on top of standard C++, the .Net folks get to do things as they like.
JimDaniel
But I don't get why they don't use a class that handles function pointers. Would be much easier/faster/etc to use them instead of the naming MOC stuff they have now.
A: 

No, there is no real way around that.

You'll have to use MOC and connect via names. But with time you'll find out that it "grows on you" and won't really bother you. Train yourself to add code in small snippets each time, testing that what you added works, and you'll have no trouble with this.

Eli Bendersky
+6  A: 

There is error detection, the connect function returns false when it fails to connect, and a warning is output on standard error (or, on Windows, to the weird place which DebugView reads from). Also you can make these warnings into fatal errors by setting QT_FATAL_WARNINGS=1 in your environment.

It's not pointless to connect by name. For example, it means that connections can be established where the signal/slot names are generated at runtime.

Intransigent Parsnip
You can also see debug output in Visual Studio's "Output" window when debugging.
Idan K
+1 - thanks for the QT_FATAL_WARNINGS - didn't know about that.
JimDaniel
A: 

I normally Practice following style of coding,

m_pCancelPushButton = new QPushButton(tr("Cancel"));
m_pCancelPushButton->setObjectName("CancelButton");


//MetaObject Connections
QMetaObject::connectSlotsByName (this);

This enable me to write code

void Class_name::on_CancelButton_clicked()
{
//Do your job here.
    reject();
}

I hope it will help you.

Thanks, Rahul

Rahul