Qt:动态小部件信号和插槽连接 [英] Qt: dynamic widgets signal and slot connection

查看:412
本文介绍了Qt:动态小部件信号和插槽连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Qt应用程序中,我动态地创建了一些小部件。其中包括需要具有类似行为的QToolButton。



以下是创建小部件的方式:

  QMap< QString,QToolButton *> unselectButtonMap; 
foreach(QString instance,...){
unselectButtonMap [instance] = new QToolButton;
myLayout-> addWidget(unselectButtonMap [instance]);
QObject :: connect(unselectButtonMap [instance],SIGNAL(clicked()),这个,SLOT(unselectInstance()));
}

现在我想要 unselectInstence slot来了解哪个实例。



我首先介绍将实例名称作为参数添加到插槽,但插槽只能从信号中获取参数。这个想法是这样的:

  QObject :: connect(unselectButtonMap [instance],SIGNAL(clicked()),this,SLOT (unselectInstance(实例))); 

有什么其他想法怎么做?



注意:根据 shan 的回答,我正在使用Qt4.8



编辑:并且因为我需要实例名称而不是QToolButton本身,这里是我来到

  void MyWindow :: unselectInstance( ){
foreach(QString instance,unselectButtonMap.keys()){
if(unselectButtonMap [instance] == QObject :: sender()){
// do stuff here ...
}
}
}

指针比较似乎工作



RE-EDIT:,根据 user1313312 的回答,我们将声明一个QSignalMapper:

  QSignalMapper * signalMapper = new QSignalMapper(this); 
QObject :: connect(signalMapper,SIGNAL(mapped(QString)),这个,SLOT(unselectInstance(QString)));

然后将QToolButton连接到映射器

  signalMapper-> setMapping(unselectButtonMap [instance],instance); 
QObject :: connect(unselectButtonMap [instance],SIGNAL(clicked()),signalMapper,SLOT(map()));

,这个插槽会简单得多:

  void MyWindow :: unselectinstance(QString instance){
// do stuff here ...
}


解决方案

尽管使用 QObject :: sender()完美的,理想主义的方法将是 QSignalMapper


In my Qt app, I create some of my widgets dynamically. Among them are QToolButtons that need to have a similar behavior.

Here is how the widgets are created:

QMap<QString, QToolButton*> unselectButtonMap;
foreach(QString instance, ...) {
  unselectButtonMap[instance] = new QToolButton;
  myLayout->addWidget(unselectButtonMap[instance]);
  QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),this,SLOT(unselectInstance()));
}

Now I would like the unselectInstence slot to know which instance is concerned.

I first thougth about giving the instance name as a parameter to the slot, but slots only take parameters from signals. The idea was something like:

QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),this,SLOT(unselectInstance(instance)));

Any other idea on how to do that ?

Notes: I'm using Qt4.8

EDIT: based on shan's answer, and because I needed the instance name instead of the QToolButton itself, here is what I came up to

void MyWindow::unselectInstance() {
  foreach(QString instance, unselectButtonMap.keys()) {
    if(unselectButtonMap[instance] == QObject::sender()) {
      //do stuff here...
    }
  }
}

The pointer comparison seems to work pretty well.

RE-EDIT: and based on user1313312's answer, we would declare a QSignalMapper:

QSignalMapper *signalMapper = new QSignalMapper(this);
QObject::connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(unselectInstance(QString)));

then connect the QToolButton to the mapper

signalMapper->setMapping(unselectButtonMap[instance], instance);
QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),signalMapper,SLOT(map()));

and the slot would be way much simpler:

void MyWindow::unselectinstance(QString instance) {
  //do stuff here...
}

解决方案

Although using QObject::sender() is perfectly fine, the idealistic approach would be QSignalMapper

这篇关于Qt:动态小部件信号和插槽连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆