Creating a slot with the same name as the auto generated one



  • Hi guys.
    I have started using QT 5.0.1 recently for a project at work, and I just realized that if I create a slot function with the exact same signature that the designer would have generated when using "Go to slot...", it gets called even if I don't explicitly assign anything in the signal/slot editor.
    It works on both window and linux with both MS and GCC tools configured.
    I assume this connection is done by the moc, but I'm not sure.

    So my question is; is this a reliable way to do it, or should I explicitly assign the signal/slot in the designer to make sure it works correctly on all platforms, and in the unforeseeable future?



  • What allows the slot to be connected is actually its name, because the file generated by uic has a call to "connectSlotsByName":http://qt-project.org/doc/qt-5.0/qtcore/qmetaobject.html#connectSlotsByName at the end of the setupUi() function.


  • Moderators

    It is done by a call to "QMetaObject::connectSlotsByName()":http://qt-project.org/doc/qt-5.0/qtcore/qmetaobject.html#connectSlotsByName method. It is not done by moc but by the uic - the ui compiler that generates ui_<yourwidget>.h files from the .ui designer files. You can take a look inside the ui->setupUi(this) method that is called in your widget constructor and see that call at the end.

    This is well established and documented feature so I would expect it to be maintained for a long time.

    I personally don't like and wouldn't recommend using this automatic connection though. It is very error prone. It is extremely easily to forget to update the slot name when you have a gazillion controls in your widget and one day decide to change the name of one of them. You won't get any compile-time errors, just a runtime debug warning that can be easily overlooked. The other thing is the naming convention - if you're, like I am, using camelCase the underscores just mess up code. Also, if the name of the control contains underscore the automatic connection might fail.



  • [quote author="Krzysztof Kawa" date="1360794269"]
    I personally don't like and wouldn't recommend using this automatic connection though. It is very error prone. It is extremely easily to forget to update the slot name when you have a gazillion controls in your widget and one day decide to change the name of one of them. You won't get any compile-time errors, just a runtime debug warning that can be easily overlooked. The other thing is the naming convention - if you're, like I am, using camelCase the underscores just mess up code. Also, if the name of the control contains underscore the automatic connection might fail.[/quote]

    Ok, so if I get this right this is actually what "Go to slot..." does.
    The "safer" alternative is to either explicitly use connect in code or explicitly make a connection in the signal/slot editor, right?

    edit:
    I did some testing and it seems that the only way to have the controls rewired automatically when changing their names is to use the designers signal/slot editor. That's pretty cool.
    Thanks


Log in to reply
 

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