What is better? connect in constructor vs. "Go to slot..." in Qt Creator



  • Hello,
    just a quick question.
    E.g. i have a form created in Qt Creator that contains some QPushButtons, QCheckboxes etc.
    Personally i prefer to use connect inside the constructor to connect signals to slots.
    (especially to connect multiple objects to one slot in a loop and use sender() to get the source of signal).

    I know that i also can be done in Qt Creator by right click on e.g. a QPushButton and use "Go to slot...".
    (this method is prefered by a colleague)

    Everyone has its own style of writing code, but are there any advantages or disadvantages
    for "using connect in constructor" vs. "Use Go to slot... from Qt Creator".

    Thx


  • Moderators

    Hi and welcome,

    Of course it doesn't matter for the compiler. It doesn't care if the code is generated or written by hand.

    The only differences are for style and maintenance:

    • "Go to slot"
      + ease of use (especially if you come from other languages with visual ui designers)
      + less code in c++ i.e. less "connect text walls".
      - enforces certain slot naming convention that might not match your own
      - susceptible to rename bugs i.e. you rename an object and the code still compiles but silently stops connecting
      - harder to find i.e. you can't just search for "connect", you need to know the name

    • manual connects
      + avoids the problems of "go to slot"
      - more boilerplate code to write


Log in to reply
 

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