[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?


  • Moderators

    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.



  • 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:
    @
    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.



  • I understand.Thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.