如何使用信号/槽机制获取发送者小部件?

发布于 2024-09-29 05:27:04 字数 93 浏览 1 评论 0原文

可以将多个信号绑定到一个插槽(不是吗?)。那么,有没有办法了解哪个小部件发送了信号呢?我正在寻找类似 .NET 中事件的 sender 参数的东西

It's possible to bind more than one signal to one slot (isn't?). So, is there a way to understand which widget sends the signal? I'm looking for something like sender argument of events in .NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

弥枳 2024-10-06 05:27:04

使用 QObject::sender()插槽,如以下示例所示:

void MainWindow::someSetupFunction( void )
{
   ...
   connect( _foobarButton, SIGNAL(clicked()), this, SLOT(buttonPressedSlot()) );
}

void MainWindow::buttonPressedSlot()
{
   // e.g. check with member variable _foobarButton
   QObject* obj = sender();
   if( obj == _foobarButton )
   { 
      ...
   }

   // e.g. casting to the class you know its connected with
   QPushButton* button = qobject_cast<QPushButton*>(sender());
   if( button != NULL ) 
   { 
      ...
   }

}

Use QObject::sender() in the slot, like in the following Example:

void MainWindow::someSetupFunction( void )
{
   ...
   connect( _foobarButton, SIGNAL(clicked()), this, SLOT(buttonPressedSlot()) );
}

void MainWindow::buttonPressedSlot()
{
   // e.g. check with member variable _foobarButton
   QObject* obj = sender();
   if( obj == _foobarButton )
   { 
      ...
   }

   // e.g. casting to the class you know its connected with
   QPushButton* button = qobject_cast<QPushButton*>(sender());
   if( button != NULL ) 
   { 
      ...
   }

}
孤芳又自赏 2024-10-06 05:27:04

QObject::sender() 就可以了工作。

QObject::sender() will do the job.

沫尐诺 2024-10-06 05:27:04

是的,您可以将多个信号连接到一个插槽。在这种情况下,您将使用 QSignalMapper 来区分信号源。该解决方案仅限于无参数信号。您可以在此处查看示例。

Yes, you can connect multiple signals to one slot. In this case you would use QSignalMapper to differentiate the sources of the signals. This solution is limited to parameterless signals. You can see an example here.

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