告警:

Slots named * are error prone [clazy-connect-by-name]

Qt 论坛回复:

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:

1
2
connect(toolButton, &QPushButton::clicked,
this, &YourClassName::nameOfYourSlot);

With this approach, your compiler will make sure that both objects, signal and slot are all correct.

通过 “Go to Slot” 自动生成的连接是有错误倾向的。如果之后在 Design 中修改了控件名称,该控件就连接不上信号了,但编译器并不会报错。

因此更好的做法是手动连接信号与槽。