Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating a slot with the same name as the auto generated one
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    drobole
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          drobole
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved