How to identify which lineEdit has changed in the ui?
-
I have a ui with 30 lineEdits,When user changes Nth lineEdit value I want to set a bool value_changed_lineEdit_N as true.
I tried
connect(ui->lineEdit,SIGNAL(textChanged(QString),this,value_changed_lineEdit=true);Is there any way to do this?Please suggest.
-
I have a ui with 30 lineEdits,When user changes Nth lineEdit value I want to set a bool value_changed_lineEdit_N as true.
I tried
connect(ui->lineEdit,SIGNAL(textChanged(QString),this,value_changed_lineEdit=true);Is there any way to do this?Please suggest.
Hi @kishore_hemmady,
you can try (untested):
connect(u->lineEdit, &QLineEdit::textChanged, this, [this]() { value_changed_lineEdit=true; });
You can make the whole thing more easy by creating the lineEdits in code and use QSignalMapper.
Your whole problem should then be solved with a few lines of code.
Regards
-
Slot has to be a function or a lambda expression. Writing "live code" like this won't work.
One approach would be to store your line edits (pointers to them) in a QVector in the same order that your N numbering has. Then you will be able to check which line edit has fired the signal (use
sender()
in your slot for that) is the Nth in your vector - and that will give you the "N" number.Or, instead of declaring 30 boolean members, do this using
QHash<QLineEdit*, bool>
and store the modification flag in the hash. -
To complete the hat-trick,
you can also subclass QLineEdit and make a new signal with an identifier:class myLineEdit : public QLineEdit { Q_OBJECT public: explicit cLineEdit(QWidget * parent = 0, int Id) : QLineEdit(parent), m_Id(id){ connect(this, SIGNAL(textChanged(QString)), this, SLOT(sendMySignal(QString)) ); } private slots: void sendMySignal(QString &str){ emit mySignal(str,m_Id); } signals: void mySignal(QString,int); private: int m_Id; };
-
To complete the hat-trick,
you can also subclass QLineEdit and make a new signal with an identifier:class myLineEdit : public QLineEdit { Q_OBJECT public: explicit cLineEdit(QWidget * parent = 0, int Id) : QLineEdit(parent), m_Id(id){ connect(this, SIGNAL(textChanged(QString)), this, SLOT(sendMySignal(QString)) ); } private slots: void sendMySignal(QString &str){ emit mySignal(str,m_Id); } signals: void mySignal(QString,int); private: int m_Id; };
@J.Hilk: Cool. Haven't though about this.
@kishore_hemmady: If you like to go @J-Hilk's way, you can use the "promote" feature in Qt Designer to change the QLineEdit's to MyLineEdit's
-
Hi,
Just one small addition to @J-Hilk suggestion: put the parent parameter as the last one. That's the common way to write constructors with multiple parameters for QObject/QWidget derived classes.