July 31, 2012

nimingzhe2008 nimingzhe2008
Ant Farmer
60 posts

[Solved]a question about connecting signal and slot

 

In book Foundations of Qt Development,it says “Trying to specify signal or slot argument values when connecting will cause your code to fail at run-time. The connect function understands only the argument types.”
I don’t understand.Since connect function understands only signal’s and slot’s arguments types .Why to specify signal or slot argument values when connecting cause your code to fail at run-time?

3 replies

July 31, 2012

sierdzio sierdzio
Area 51 Engineer
2314 posts

In Qt4 (there is a change here in Qt5…) signal connection are parsed by MOC (meta-object compiler), and stored as strings. Connections are made at run-time using those strings. I’m not sure it actually fails, but it certainly might.

 Signature 

(Z(:^

July 31, 2012

Andre Andre
Area 51 Engineer
6031 posts

Yes, it actually fails.

Many people try to do things like connecting a signal without an argument to a slot with an argument given in the connect statement, but that is impossible. So, this does not work:

  1. connect(myButton5, SIGNAL(clicked()), this, SLOT(mySlot(5))); // Fail!

What’s more, you cannot even use the argument names in the connect call, only the argument types or the connect will fail. Here at work we use our own connect function, a thin wrapper around connect. It asserts if the connect fails, and it catches these kinds of problems before we release code to customers.

Note that in Qt 5, there are template-based connect statements that even work with lambda expressions (if you use C++11). That way, you actually can make connect statements like the above one, though they look a bit different of course.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 2, 2012

nimingzhe2008 nimingzhe2008
Ant Farmer
60 posts

I understand.Thank you very much.

 
  ‹‹ how to use qmake with -qtnamespace modified frameworks      [SOLVED] Apply general shader on whole QGLPainter ››

You must log in to post a reply. Not a member yet? Register here!