gpt4 book ai didi

c++ - Qt5:连接:如果插槽的参数少于信号,如何使用 'connect'

转载 作者:搜寻专家 更新时间:2023-10-31 00:07:46 24 4
gpt4 key购买 nike

任务是从一个带有一个参数的信号连接到一个带有零参数的插槽。使用“旧的”Qt4 连接方式,它的工作方式如下

connect(object1, SIGNAL(signal(int param)), object2, SLOT(slot()))

但是如果我想使用类型安全的 Qt5 连接怎么办?

文档只提到了以下情况:

  • 连接到槽中的默认参数

  • 并采用上述stringbased connect的方式。

最佳答案

Qt5 风格的连接机制会忽略信号的额外参数,就像您可以在 Qt4 风格中指定的那样:

The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches when using the function pointer-based syntax. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime.

来自Qt Docs (强调我的)。

所以你可以像这样使用新样式:

connect(&object1, &ClassObject1::signal, &object2, &ClassObject2::slot);

关于c++ - Qt5:连接:如果插槽的参数少于信号,如何使用 'connect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52387925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com