Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why is it that on_button_clicked() doesn't need a signal?



  • Hi experts,

    I have a on_<button objectName>_clicked() slot function and somehow it is triggered every time a button is clicked, without any signals defined.

    Now that the button is definitely the most used widget so maybe this is automated under the hood, I still would like know the mechanism, and more importantly which widgets have the same behavior. For example, QTGraphicsView doesn't work this way.

    I already consulted the signal-slot page as well as the QTPushButton page but didn't find what I need.

    Thanks!


  • Lifetime Qt Champion

    Hi,

    This is done by QMetaObject::connectSlotsByName which is called as part of the you designer ui object setup method.


  • Lifetime Qt Champion

    Hi
    When you us UI files, the code generated in setupUI calls
    QMetaObject::connectSlotsByName(MainWindow);

    which will try to match widgets and slots

    https://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName

    hehe one sec too late :)

    In any case. Its far better to use connects in the code as this method suffer from easy to break.

    if you ever rename your widget or the slot - it stops working. silently.
    So for a production-quality app, its best to avoid this feature however handy it is.


Log in to reply