what does 'Slots named on_foo_bar are error prone' mean?
-
Hi,
When I use the "Go to slot" from qt creator design window to generate slots automaticly.
I got warning "Slots named on_foo_bar are error prone"
when there is only one this warning, everything seems to be fine.
but after I got two warning, the second button can not trigger the slot.
the name of first button is "toolButton" and the second button named "pushButton"
when I change the "pushButton" to "pushButton_1" and re-generate slot, finaly the pushButton can trigger the slot.Thank you .
-
So the clazy warning has warned you about exactly the issue you encountered: when you use "Go to slot" Qt uses an automated connection mechanism called
connectSlotsByName()
where it uses some conventions to match objects, signals and slots. As clazy reports, and as you have experienced: this is convenient but error prone. It's very easy to make a mistake with this approach.To do this properly, use manual signal-slot connections with new connect syntax, like so:
connect(toolButton, &QPushButton::clicked, this, &YourClassName::nameOfYourSlot);
With this approach, your compiler will make sure that both objects, signal and slot are all correct.
-
i think the real question should have been "If there is an option to do this in designer, and everyone is saying, don't use it, then why even have it?" Wouldn't it just be better to remove the feature--it gives a bad impression when a product has features that cause its own design rules to kick in, just seems like the user should be prompted instead.
-
@osirisgothra because Qt is old and already has a ton of production code out there in the wild. Suddenly removing functionality is not so easy, needs to go through deprecation first and then removal much later, in a major release - so earliest in Qt 7.
On the other hand -
connectSlotsByName()
is convenient and useful. Some people may prefer it, or use it only in limited cases like in small test example / test / POC apps, just to code them quicker. Also at some point I think QML used this feature internally. Maybe I'm wrong though.