signalMapper = new QSignalMapper(this); QGridLayout *gridLayout = new QGridLayout; for (int i = 0; i < texts.size(); ++i) { QPushButton *button = new QPushButton(texts[i]); connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); signalMapper->setMapping(button, texts[i]); gridLayout->addWidget(button, i / 3, i % 3); } connect(signalMapper, SIGNAL(mapped(const QString &)), this, SIGNAL(clicked(const QString &))); setLayout(gridLayout);
signalMapper = new QSignalMapper(this); QList<QPushButton *> buttonList; QGridLayout *gridLayout = new QGridLayout; for (int i = 0; i < texts.size(); ++i) { QPushButton *button = new QPushButton(texts[i]); button->setObjectName(QString("btn%1").arg(i)); connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); signalMapper->setMapping(button, texts[i]); gridLayout->addWidget(button, i / 3, i % 3); buttonList.append(button); } connect(signalMapper, SIGNAL(mapped(const QString &)), this, SIGNAL(clicked(const QString &))); setLayout(gridLayout);
class WhiteRabbit: public QObject { Q_OBJECT public slots: void follow(const QString &path) { //получил свой путь? - ищи крольчишку } }//.... QMap<QString, QString> textsMap; textsMap.insert("Калькулятор", "calc.exe"); textsMap.insert("Консоль", "cmd.exe"); signalMapper = new QSignalMapper(this); QHBoxLayout *layout = new QHBoxLayout; for(QMap<QString, QString>::ConstIterator it = textsMap.constBegin(); it != textsMap.constEnd(); ++it) { QPushButton *button = new QPushButton(it.key()); connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); signalMapper->setMapping(button, it.value()); layout->addWidget(button); } WhiteRabbit* whiteRabbit = new WhiteRabbit; connect(signalMapper, SIGNAL(mapped(const QString &)), whiteRabbit, SLOT(follow(const QString &)));