如何从QMAP中从QTimer对象发送的超时信号从哪个键中找到?
在一个函数中,如何理解我在qmap中创建的qTimer对象发送的信号,如何从插槽函数中从哪个对象找到信号。
我在代码上方创建了QMAP sql.h
public slots:
void experiment();
void run();
private:
QMap<QString,QTimer*> job;
i使用qTimer创建QMAP值和键。 SQL.CPP
void SQL::experiment()
{
QTimer *timer=new Qtimer();
job.insert("dd",timer);
QTimer *timer1=new Qtimer();
job.insert("ss",timer1);
job.value("dd")->start();
job.value("dd")->setInterval)(5000);
job.value("ss")->start();
job.value("ss")->setInterval)(10000);
connect(job.value("dd"),SIGNAL(timeout()),this,SLOT(run()));
connect(job.value("ss"),SIGNAL(timeout()),this,SLOT(run()));
}
在此插槽中,我如何了解当时QMAP中哪个QTIMER接收信号?
void SQL::run()
{
//job.value(key) // how to understand key
}
我以为我可以将Sender()与Qmapiterator一起使用,但是我找不到。你能帮忙吗?
In a function, how to understand the signal sent from Qtimer objects that I created in Qmap, how to find from which object the signal comes from in the slot function.
I created Qmap above code
SQL.h
public slots:
void experiment();
void run();
private:
QMap<QString,QTimer*> job;
I create QMap value and key with Qtimer.
SQL.cpp
void SQL::experiment()
{
QTimer *timer=new Qtimer();
job.insert("dd",timer);
QTimer *timer1=new Qtimer();
job.insert("ss",timer1);
job.value("dd")->start();
job.value("dd")->setInterval)(5000);
job.value("ss")->start();
job.value("ss")->setInterval)(10000);
connect(job.value("dd"),SIGNAL(timeout()),this,SLOT(run()));
connect(job.value("ss"),SIGNAL(timeout()),this,SLOT(run()));
}
In this slot, how can I understand which of the Qtimer in the Qmap receives a signal at that time?
void SQL::run()
{
//job.value(key) // how to understand key
}
I thought I could use sender() with Qmapiterator, but I couldn't find out how. can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
旧方法使用
QSignalMapper
。您还可以将密钥设置为计时器对象的动态属性,因此您可以通过Qobject :: sender()
访问它。但是,今天您可能应该只使用lambda。首先,更改运行插槽以获取所需的任何参数:
然后,只需使用lambda轻松将所需的参数
作为附带说明,您不应使用旧插槽()宏,除非您确实出于某种原因需要。使用“新”(10岁)连接语法。
Old way is using
QSignalMapper
. You could also set the key as dynamic property of the timer object, so you could access it throughQObject::sender()
. But, today you should probably just use a lambda.First, change the run slot to take any parameters you want:
Then, just use lambda to easily pass the required parameters
As a side note, you shouldn't use the old
SIGNAL()
andSLOT()
macros unless you really have to for some reason. Use the "new" (10 years old) connect syntax.