Qt World Summit: Submit your Presentation

Noob anomaly - ghost signal emission

  • Hi

    Well I'm aware that I haven't spent much time in qt and its docs, but the latest magic 'anomaly' I've faced pushed me into corner:P

    1. I have QWidget in mainwindow.ui, promoted to my custom FileCommander class which implements QTreeview and QFileSystemModel.
    2. Still, above things are part of my mainwindow, a widget with treeview, but a bit separated for better management.
    3. Once I decided to make an event 'clicked' on QTreeView
    4. Then I thought to myself, damn, I need to get that event in my mainwindow, so I moved generated FileCommander_clicked() to MainWindow in order to connect and I removed slot from FileCommander at all:P
    5. My mind blowed up when I saw it working without additional connect (I used to write delegates/etc in the past).

    Well as far as I know signals are emited in such fashion, the problem is that I can't sort out that sick behavior:P By adding just a proper slot that matched myClass_clicked in other class I did catch the signal. I feel like it could drive me into trouble in future and I consider changing it's name and connect manually to know what's going on:P

    Could someone tell me where/when it happens that this slot receives emission in mainwindow without any explicit declaration? (it feels like an event going from the bottom to top in some standard messaging system... anyway)
    Second thing, do you advise to change its name and connect manually to keep things more readable though the name seems to be self explainatory ? :P

  • Moderators

    You've experienced the horror/ beauty of connectSlotsByName method. It is invoked in ui_*.h header which is generated by UIC when you compile your project.

  • Lifetime Qt Champion

    In setupUI (), the generated code calls
    So if name matches the "auto" syntax then its made.

    I can only recommend NOT using auto connect feature and use the new
    syntax to manually hook up widgets to slots because using auto,

    • If you rename widgets, it will break and
      also renaming slot breaks code
      so the auto connect feature is best left for prototyping etc.

    Using the new syntax, you would discover at first compile that you broke the click of a button.
    otherwise, its a silent fail which never is really desirable for a real application.


  • Straight to the point. Thank you guys!

  • Moderators

    @Pepas an easy way to prevent accidental connectSlotsByName connects would be the use of proper CamelCase notation like every self-respecting c++ programmer should :P

Log in to reply