带默认参数的 Qt 插槽

发布于 2025-01-08 00:24:45 字数 409 浏览 4 评论 0原文

我有一个旋转控件块,可以更改数组的各个元素 我不想拥有单独的接收器插槽函数,而是只想指定哪个控件在信号中发送消息

您可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少心凉 2025-01-15 00:24:45

在任何插槽处理程序中,您都可以使用 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.

北城挽邺 2025-01-15 00:24:45

我不这么认为,至少不使用该语法... SIGNAL 和 SLOT 宏将它们的参数转换为字符串,然后由 Qt 运行时解析和使用在编译的预处理阶段在 moc 创建的表中查找关联的函数和/或类方法。因此,如果您将默认参数编码到 SLOT 宏中,那么这不是 Qt 可用于在 moc-生成的函数表。

I don't believe so, at least not using that syntax ... the SIGNAL and SLOT 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 by moc during the pre-processing phase of compilation. So if you encoded a default argument into the SLOT 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 the moc-generated function tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文