How to treat multiple QLineEdit

  • Hello,

    In my .ui file I have built a window where I have a lot of QLineEdit, as I need to get several single char inputs from the user (42 to be precise). I wanted to know if there was a way to change the following code to make it neater (with a loop possibly):


    In my case I want to replace the contents of the QString str with the inputs that I read from the QLineEdit boxes.
    I know I could make a single QLineEdit 42 characters long, but for the moment I would like to stick with 42 single char QLineEdit boxes.

    Any pointers?
    Thank You

  • First think comes into my mind is to group all and only your QLineEdits for example into a QWidget or QFrame, then you could try with a loop:

    @QObject *le;
    int n=0;
    foreach ( le, ui->your_new_QWidget_group.children() ) {
    str.replace(n,1, static_cast<QLineEdit *>( le )->text() );


  • I thought of that, but will I be able to access and control each member individually?

  • yes, like you did with your first post:


  • I guess you create those 42 line edits programmatically (and not by dragging 42 widgets in QtDesigner), so it should be no problem of obtaining a QList<QLineEdit*>. Then just iterate through the list and append the respective char to an empty string.

    //EDIT: Hah, well, you got answers already... obviously it took me 40 minutes to get to this tab in my browser ;)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.