QT5 C++槽函数的两种connect连接方式的区别,建议使用现代的连接方式

QT5 C++槽函数的两种connect连接方式的区别,建议使用现代的连接方式

(1)QT5 C++ 槽函数定义
class MainWindow : public QMainWindow {
Q_OBJECT
// ......
private slots:
void slotSplitterMoved(int, int);
// ......
}

(2)使用旧的字符串connect连接方式,容易报错或者警告
连接 splitterMoved 信号到 handleSplitterMoved 槽,如果使用下面旧的字符串连接方式,需要确保信号和槽的签名没有多余的空格,否则容易报错或者警告。
connect(g_pSplitterMain.get(), SIGNAL(splitterMoved(int, int)), this, SLOT(slotSplitterMoved(int, int)));

上面代码编译时警告:mainwindow.cpp:218:36: Signature is not normalized. Use splitterMoved(int,int) instead of splitterMoved(int, int) [clazy-connect-not-normalized]
在你的代码中,提示你将 splitterMoved(int, int) 改为 splitterMoved(int,int),这是因为 Qt 的 connect 函数要求信号和槽的签名没有多余的空格。

(3)建议使用现代的连接方式
chatGPT建议使用现代的连接方式:
connect(g_pSplitterMain.get(), &QSplitter::splitterMoved, this, &MainWindow::slotSplitterMoved);

 

posted on   patton88  阅读(134)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示