Menu菜单 或 其中的QAction 连接槽函数
以下两句connect 任一均可实现:
1 QStringList strList;
2 for(int i = 0; i< strList.size(); ++i)
3 {
4 QString tmp = strList.at(i);
5 QAction *act = new QAction(tmp, m_menu);
6 act->setCheckable(true);
7 act->setChecked(false);
8 m_menu->addAction(act);
9 connect(act, SIGNAL(triggered()), this, SLOT(checkedAction()));
10 }
11 //connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(checkedLibAction(QAction*)));
QSignalMapper
1 signalMapper = new QSignalMapper(this);
2
3 for (int i = 0; i < texts.size(); ++i)
4 {
5 QPushButton *button = new QPushButton(texts[i]);
6 connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
7 signalMapper->setMapping(button, texts[i]);
8 }
9 connect(signalMapper, SIGNAL(mapped(QString)), this, SIGNAL(clicked(QString)));
QSignalMapper 官方介绍:
The QSignalMapper class bundles signals from identifiable senders.
1 void setMapping(QObject *sender, int id);
2 void setMapping(QObject *sender, const QString &text);
3 void setMapping(QObject *sender, QWidget *widget);
4 void setMapping(QObject *sender, QObject *object);
5 void removeMappings(QObject *sender);
6
7 Q_SIGNALS:
8 void mapped(int);
9 void mapped(const QString &);
10 void mapped(QWidget *);
11 void mapped(QObject *);
这四种捆绑方式,使用超级灵活。同一个sender在一个map中可以被捆绑多次;
int 型的以及 QString、QWidget等 的Map捆绑互相独立,互不影响。
以及一个 signalMap 的 mapped信号最多可以连接到4个不同类型的槽函数,这四个信号槽相互独立。