Qt编程过程出现 QMetaObject::connectSlotsByName: No matching signal for 问题分析以及解决过程方法

本文详细解析了QMetaObject::connectSlotsByName警告的产生原因,介绍了如何遵循命名规范避免警告,以及如何手动使用connect函数建立信号与槽的连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现问题: QMetaObject::connectSlotsByName: No matching signal for

分析问题:参考了这个文章https://blog.youkuaiyun.com/u012997311/article/details/51313992

After a bit of sleuthing(侦察) to find the source of all the Qt warnings, “QMetaObject::connectSlotsByName: No matching signal for …”, in our log window/file, here’s what I’ve found.

setupUi calls connectSlotsByName(setupUi函数调用connectSlotsByName函数). connectSlotsByName parses(分析) the moc_ file in order to connect slots and signal s. The moc_ file contains a list of all the slots for the class. The parser iterates over the list of slot names looking for the following pattern: on_objectName_signal , where on_objectName_signal is the name of the slot, objectName is the object name and signal is the signal . For example, if you have a slot named, on_doneButton_clicked(), the parser looks for an object named doneButton, a signal named clicked and then connects the on_doneButton_clicked() slot to QButton’s signal clicked().

If you follow this naming convention, you do no t need to call the connect() method, no r do you need to connect the signal via the Qt UI editor in VisualStudio. connectSlotsByName will automatically make the connection for you.

So, in order to get rid of the “No matching signal for …” warnings, we need to either follow this naming convention, or make sure no ne of our slot names begin with “on_”. If you have a slot, onDoneButton_clicked, for example, connectSlotsByName will no try to connect it with a signal , no r will emit an warning. 

大概意思就是:用IDE里的QtEditer可以自动调用Qt中“connectSlotsByName”即“按空间名称关联槽”的方式进行关联,对应的函数必须写成“on_控件名_信号名”的格式;或者也可以通过connet函数人为显式地将信号和槽关联起来。但是,如果采用显式 connect的方法的同时,又将槽函数的名字起成了“on_控件名_信号名”的格式,那么就会在运行时弹“QMetaObject::connectSlotsByName: No matching signal for”的警告了!

  1. 如果在一个QObject子类里定义了"on_"开头的槽的话,一定会被connectSlotsByName函数进行搜索匹配的操作的 。
  2. 同名connectSlotsByName只能给其中一个建立缺省的信号和的连接 。
  3. 出现大量编码创建大量控件的情况,最好是自己去建立信号和槽的连接,而不是依赖connectSlotsByName来做到这个工作。
  4. connectSlotsByName更适合的任务是与desinger配合完成缺省的信号和槽的连接。
  5. ui->setupUi(this);放到了控件创建之后运行,objectName方法建立连接。

解决问题:定义信号的槽函数,命名的函数名字不要使用on_开始。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值