tags:

views:

190

answers:

5

Hello.

In C# in order to connect a signal to slot (delegate to method) all i do is:

first.signal += second.slot

But in Qt it is:

connect( & first, SIGNAL( signal( int, int, QString ) ), & second, SLOT( slot( int, int, QSTring ) ) );

And this one is counted short, normally they are spawned 2-3 lines. Of course i fully understood that Qt mocks only .h files and for .cpp they are bound to standard syntax, but is it any tricks / ways to make signal-slot connectons a bit shorter? I know about auto-connecting signals to slots by giving them special names, but this works only for signals in .ui file. Any hints?

+3  A: 

If you find that long and repetitive, you could use macros to make the syntax lighter. For example:

#define S_i_i(x) x(int, int, QString)
#define CONNECT(sx,x,tx,sy,y,ty) connect(&x, SIGNAL(tx(sx)), &y, SLOT(ty(sy)))

CONNECT(signal, first, S_i_i, slot, second, S_i_i)

The drawback is that your code will lose in readability for others unless you choose your macros carefully.

WildSeal
I think it's debatable to say that this syntax is lighter... Using this approach you would need a #define for each signal signature. The only place where this `might` be useful would be if you need to connect multiple slots to the same signal, and in that case you've already written one connect(...) and a simple copy/paste will do.
Idan K
@daniel: what the OP wanted is _shorter_ code. There aren't that many signatures, and they could be defined once for all in a common header, like for common delegates in C#. That being said, I don't find the original Qt way of coding signals and slots annoying in the first place and would rather stick to it, but that's just a matter of personal preference :)
RedGlyph
+1  A: 

Personally I don't think it's that bad. You only have to write it once for each signal/slot and most of the time you don't have to touch it afterwards.

The closest thing I found that helps in someway is using boost::bind/function with the signals/slots mechanism. It doesn't provide a direct answer to your question but it is related and you might make use of it. You can read about it here.

Idan K
+3  A: 

If you are doing connections from a signal in a different object to a slot in this object, you can do a shortcut, and use this:

connect( & first, SIGNAL( signal( int, int, QString ) ), SLOT( slot( int, int, QSTring ) ) );

instead of this:

connect( & first, SIGNAL( signal( int, int, QString ) ), & second, SLOT( slot( int, int, QSTring ) ) );

which removes a little bit of the connection. There are other shortcuts available as well, I believe. However, your general point is valid, and is mostly a side-effect of how Qt does their signal/slot mechanism. If you really want a more terse syntax in C++, you could look at either the Boost::Signals or SigC++ libraries, which also provide a signal/slot mechanism. Be aware of syntax conflicts, however.

Caleb Huitt - cjhuitt
Boost::Signals don't integrate well in Qt, unfortunately :(
Eye of Hell
+6  A: 

I know about auto-connecting signals to slots by giving them special names, but this works only for signals in .ui file

That approach can be used outside of .ui files, you can call QMetaObject::connectSlotsByName anywhere you want.

Intransigent Parsnip
Just be careful how and when you call it... if you do that in a base-class constructor, then again in a derived-class constructor (for instance), you might end up with some slots connected twice. It doesn't distinguish those connected already when it is run. (You can probably guess how I now know that.)
Caleb Huitt - cjhuitt
+1  A: 

One trick would be to use QtCreator as an editor, as it lets you pick signals and slots from a drop-down-list as you type.

A seemingly unrelated reflection is that Qt strives to have a rather verbose API, as it is easier to read (and to remember when writing). Thus, they avoid abbreviations and write what they mean: QAbstractItemModel, setWindowTitle, etc. Not QAbstItModel, setWinTit, etc. More typing, less misunderstandings and linker errors (missing symbols)

e8johan
QtCreator can only connect Gui signals and slots, or any one?
Eye of Hell
When typing "connect(src, SIGNAL(", you get a list of signals available... same for SLOT(...
e8johan