//Code Begins
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create buttons
//set the properties as required.
connect(btn , SIGNAL(clicked()), m_btnSigMapper, SLOT(map()));
//now create unique name for your widget
m_btnSigMapper->setMapping(btn , szMapName);
}
//now this is important connect
//Here we connect to our slot whenever a button is clicked.
connect(m_btnSigMapper,
SIGNAL(mapped
(const QString &)),
this,
SLOT(slotBtnClicked
(const QString &)));
//slotBtnClicked function
void slotBtnClicked
(QString szMapName
) {
//do the functionality based on the map name because it is unique.
}
////// A sample code for your problem statement can be like below.
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create line edits
//now set a name to your line edit.
lineEdit.
setObjectName(QString("LineEdit").
append(QString::number(nIndex
)));
//set the properties as required.
//Connect textEdited signal.
connect(lineEdit ,
SIGNAL(textEdited
(const QString &)), m_lineEditSigMapper,
SLOT(map
()));
m_lineEditSigMapper->setMapping(lineEdit , lineEdit);
}
//now this is important connect
//Here we connect to our slot whenever a text is edited in your line edits.
connect(m_lineEditSigMapper,
SIGNAL(mapped
(QWidget*)),
this,
SLOT(slotLineEditTextEdited
(QWidget*)));
//Create a variable which stores the values from LineEdits.
QMap<QString,QString> m_lineEditValues; //class variable
//slotLineEditTextEdited function
void slotLineEditTextEdited
(QWidget* widget
) {
QLineEdit *lineEdit
= reinterpret_cast<QLineEdit
*>
(widget
);
m_lineEditValues.insert(lineEdit.objectName(),lineEdit.text());
}
//Code Begins
QSignalMapper *m_btnSigMapper; //its a class variable
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create buttons
QPushButton *btn = new QPushButton();
//set the properties as required.
connect(btn , SIGNAL(clicked()), m_btnSigMapper, SLOT(map()));
//now create unique name for your widget
QString szMapName = QString::number(nIndex);
m_btnSigMapper->setMapping(btn , szMapName);
}
//now this is important connect
//Here we connect to our slot whenever a button is clicked.
connect(m_btnSigMapper, SIGNAL(mapped(const QString &)), this, SLOT(slotBtnClicked(const QString &)));
//slotBtnClicked function
void slotBtnClicked(QString szMapName)
{
//do the functionality based on the map name because it is unique.
}
////// A sample code for your problem statement can be like below.
QSignalMapper *m_lineEditSigMapper; //its a class variable
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create line edits
QLineEdit *lineEdit = new QLineEdit();
//now set a name to your line edit.
lineEdit.setObjectName(QString("LineEdit").append(QString::number(nIndex)));
//set the properties as required.
//Connect textEdited signal.
connect(lineEdit , SIGNAL(textEdited(const QString &)), m_lineEditSigMapper, SLOT(map()));
m_lineEditSigMapper->setMapping(lineEdit , lineEdit);
}
//now this is important connect
//Here we connect to our slot whenever a text is edited in your line edits.
connect(m_lineEditSigMapper, SIGNAL(mapped(QWidget*)), this, SLOT(slotLineEditTextEdited(QWidget*)));
//Create a variable which stores the values from LineEdits.
QMap<QString,QString> m_lineEditValues; //class variable
//slotLineEditTextEdited function
void slotLineEditTextEdited(QWidget* widget)
{
QLineEdit *lineEdit = reinterpret_cast<QLineEdit*> (widget);
m_lineEditValues.insert(lineEdit.objectName(),lineEdit.text());
}
To copy to clipboard, switch view to plain text mode
Cheers...
Bookmarks