先看代码
cpp
SdBatchTool::SdBatchTool(QWidget *parent) : QMainWindow(parent) {
ui.setupUi(this);
init_ui();
print_log("工具启动......");
}
SdBatchTool::~SdBatchTool() {}
void SdBatchTool::init_ui() {
//connect(ui.btn_identy, &QPushButton::clicked, this,
// &SdBatchTool::on_btn_identy_clicked);
//connect(ui.btn_select_p8b, &QPushButton::clicked, this,
// &SdBatchTool::on_btn_select_p8b_clicked);
//connect(ui.btn_export, &QPushButton::clicked, this,
// &SdBatchTool::on_btn_export_clicked);
connect(this, &SdBatchTool::print_log, this, &SdBatchTool::on_print_log);
}
void SdBatchTool::on_btn_identy_clicked() {}
void SdBatchTool::on_btn_select_p8b_clicked() {
p8b_path = QFileDialog::getExistingDirectory(
this, "选择指纹文件导出文件夹", QString(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!p8b_path.isEmpty()) {
ui.le_p8b_path->setText(p8b_path);
}
}
我在界面上加了一个QPushButton,ID是btn_identy,槽函数为void on_btn_identy_clicked();
我手动加了connect函数。加完后运行,发现点击QPushButton一下,会触发两遍槽函数。
后来我把connect注释掉,发现QPushButton的槽函数还是会被触发,但是只触发一遍,正常了。
这时我才发现,原来Qt中 如果槽函数命名为on_<QPushButton ID>_clicked,那么就可以免写connect,默认就已经连接了。
所以我手动写了一遍connect,反而重复连接,所以才会触发两遍。