QLineEdit Find child crashes
-
Hello. How do I find a child of lineEdit created in designer. This is what I tried, but it crashes
QLineEdit *buttonx = this ->findChild<QLineEdit *>("lineEdit_8", Qt::FindDirectChildrenOnly)->setText("Some Text"); QLineEdit *button = parentWidget()->findChild<QLineEdit *>("lineEdit_8", Qt::FindDirectChildrenOnly)->setText("Some Text"); QLineEdit *buttonx = centralWidget()->findChild<QStackedWidget *>("stackedWidget_2")->findChild<QGroupBox *>("groupBox_4")->findChild<QLineEdit *>("lineEdit_8")->setText("Some Text");
There is Vertical Layout, inside that there is stackedWidget, inside the stackedWidget there is a groupbox, then horizontal layout. the lineEdit is inside horizontal layout. How can I access this? Thanks
-
The put all your QLineEdit pointer into a QVector and iterate over them.
-
Hi,
Since you are using designer, all the widgets are accessible through the
ui
object so why do you want to use findChild ? -
Hi,
Since you are using designer, all the widgets are accessible through the
ui
object so why do you want to use findChild ?@SGaist I would like to send the names of lineEdits on button click, I have a function to process many qlineEdits and I wouldn't like to type each ui->lineEdit. My solution was to only send the name and based on the name apply something to that specific lineEdit.
-
Send the names of linedits ? Can you give a bit more details about your goal ?
-
@SGaist for example:
I have a function processLineEdits(QString lineEditname) and a private slot on_pushButton_3_clicked()
void someClass::on_pushButton_3_clicked(){ processLineEdits(lineEdit->objectName()) } void someClass::processLineEdits(QString lineEditname){ QLineEdit *buttonx = this ->findChild<QLineEdit *>(lineEditname, Qt::FindDirectChildrenOnly); buttonx ->setText("Some Text"); }
-
And why not simply pass the QLineEdit pointer here?
-
And why not simply pass the QLineEdit pointer here?
@Christian-Ehrlicher I have many lineEdits and many slots each slot sends one lineEdit name to the processLineEdits(). This function receives any lineEdit name checks the echoMode if is normal then sets to password and if is password then it sets to normal. It shows and hides text. With something like this I don't need to do it one lineEdit by one, I want the function to handle for all qLineEdit by name.
-
The put all your QLineEdit pointer into a QVector and iterate over them.
-
Wouldn't it better to create a custom widget containing your lined edits and buttons and give it a proper interface to do what you want to do ?
-
The put all your QLineEdit pointer into a QVector and iterate over them.
@Christian-Ehrlicher Thanks, it worked.