如何从QMAP中从QTimer对象发送的超时信号从哪个键中找到?

发布于 2025-01-22 17:53:39 字数 872 浏览 5 评论 0原文

在一个函数中,如何理解我在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 技术交流群。

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

发布评论

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

评论(1

苏别ゝ 2025-01-29 17:53:39

旧方法使用QSignalMapper。您还可以将密钥设置为计时器对象的动态属性,因此您可以通过Qobject :: sender()访问它。但是,今天您可能应该只使用lambda。

首先,更改运行插槽以获取所需的任何参数:

void SQL::run(const QString &key)
{
    QTimer *timer = job.value(key);
}

然后,只需使用lambda轻松将所需的参数

QString name="ff";
connect(job.value(name), &QTimer::timeout, this, [this, name]() { 
            run(name); 
        });
        // name is capture by value above,
        // so changing name variable later does not
        // affect the value captured by the 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 through QObject::sender(). But, today you should probably just use a lambda.

First, change the run slot to take any parameters you want:

void SQL::run(const QString &key)
{
    QTimer *timer = job.value(key);
}

Then, just use lambda to easily pass the required parameters

QString name="ff";
connect(job.value(name), &QTimer::timeout, this, [this, name]() { 
            run(name); 
        });
        // name is capture by value above,
        // so changing name variable later does not
        // affect the value captured by the lambda

As a side note, you shouldn't use the old SIGNAL() and SLOT() macros unless you really have to for some reason. Use the "new" (10 years old) connect syntax.

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