带默认参数的 Qt 插槽
我有一个旋转控件块,可以更改数组的各个元素 我不想拥有单独的接收器插槽函数,而是只想指定哪个控件在信号中发送消息
您可以使用 QSignalMapper 来完成此操作 - 但是否可以像下面那样简单地执行此操作?
spin0 = new QDoubleSpinBox;
connect(spin0,SIGNAL(valueChanged(double)),this,SLOT(handler(0,double));
spin1 = new QDoubleSpinBox;
connect(spin1,SIGNAL(valueChanged(double)),this,SLOT(handler(1,double));
....
private slot:
void handler(int element,double value);
I have a block of spin controls which change individual elements of an array
Rather than having separate receiver slot functions, I wanted to just specify which control sent the message in the signal
You can do this with a QSignalMapper - but is there anyway of doing it simply as below?
spin0 = new QDoubleSpinBox;
connect(spin0,SIGNAL(valueChanged(double)),this,SLOT(handler(0,double));
spin1 = new QDoubleSpinBox;
connect(spin1,SIGNAL(valueChanged(double)),this,SLOT(handler(1,double));
....
private slot:
void handler(int element,double value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在任何插槽处理程序中,您都可以使用 sender()获取指向发送信号的对象的指针。然后您可以使用 objectName() 属性传达任何进一步的识别信息。
From any slot handler you can can use sender() to get a pointer to the object that sent the signal. Then you can use the objectName() property to communicate any further identifying information.
我不这么认为,至少不使用该语法... SIGNAL 和 SLOT 宏将它们的参数转换为字符串,然后由 Qt 运行时解析和使用在编译的预处理阶段在
moc
创建的表中查找关联的函数和/或类方法。因此,如果您将默认参数编码到SLOT
宏中,那么这不是 Qt 可用于在moc-生成的函数表。
I don't believe so, at least not using that syntax ... the
SIGNAL
andSLOT
macros turn their arguments into strings which are then parsed and used by the Qt runtime to look-up the associated functions and/or class methods in the tables created bymoc
during the pre-processing phase of compilation. So if you encoded a default argument into theSLOT
macro, then that's not a valid function signature that can be used by Qt for run-time lookup of the actual slot function in themoc
-generated function tables.