Nominate our 2022 Qt Champions!

[Solved] Reading characters from QInputDialog and concatenating/append

  • Goodday,

    I am writing a program that asks a user to enter their name and surname in a QInputDialog. When the user presses the ok button a QMessageBox pops up with their username and password.

    I need assistance with the following

    When a user enters their name and surname, the program has to take the first 4 letters of their surname and first letter of their name and create a username.If the surname is less then 4 letters it needs to take letters from the name.

    For example:

    john black


    I cannot figure out how to read the surname and concatonate it with the first letter of the name.

    Some guidance would be really appreciated as I am very new to QT ( 2 weeks).


  • Lifetime Qt Champion


    You're looking for "QString::left(int n)":

  • The name and surname will have variable lengths. So I would need to get the first 4 characters of the surname each time.

    john black


    peter appendix


    So the code needs to be able to handle each name and surname.


  • I was looking at count 4 letters after the space between the name and surname , so as soon as the code sees the space it counts 4 letters and stores it. Then appends the first letter of the first name to the end of the four letters.

    I am not sure if I am making this more complex then it is.

  • Lifetime Qt Champion

    Yes you are

    bool ok;
    QString fromDialog = QInput::getText(//your settings);

    if (ok && !fromDialog.isEmpty()) {
    QStringList elements = fromDialog.split(" ");
    username = elements[0].left(4) + elements[1].left(1);

    Note that this make several assumptions like your user will indeed input two elements, that they are in the right order and of sufficient length.

    Since your user must give such information, you should rather do a dialog where it's clear what they must give you otherwise you'll get user that give first name then last name and other who will do it in the other order

  • So I am having 2 issues with my code:

    The one is displaying the username and password in the QMessagebox and the other is generating the random password from the appended string.

    I am sure it is some small issues but I am not picking it up. I am using QT 5.3.0

    Here is my code:

    @// Declarations of variables
    bool ok;
    QString fullName;
    QString userName;
    QString passWord;
    QString genPass;

     fullName = QInputDialog::getText(0,"Username","Please Enter Name and Surname:", QLineEdit::Normal," ", &ok);
     if (ok && !fullName.isEmpty()) {
     QStringList elements = fullName.split(" ");
     userName = elements[0].left(4) + elements[1].left(1);
     //If username is less than five characters
     while (userName.length() < 5) {
         userName += '0';
     //Remove all whitespaces from string
     userName = userName.simplified();
     userName.replace( " ", "" );
     //First letter of username to uppercase
          userName = " ";
          userName.replace(0, 1, userName[0].toUpper());
          passWord = userName;
          //For loop to generate random password of 5 characters from concatenated name/surname string
          for (int i = 0; i < 5; i++){
              passWord[i] = genPass [qrand() % (sizeof (genPass) -1)];
                 passWord[i] = genPass [qrand() % (sizeof (genPass) -1)];
                }while (passWord[i] = ' ');
            //QMessageBox to display username and password
                    QString response = QString(" Username: %1 \n Password: %2") .arg(userName).arg(passWord);
                     QMessageBox::question(0, "Username and Password", response, QMessageBox::Ok);

    return EXIT_SUCCESS;

  • Hi SGalst, is there a better way to do the password generator.

  • Lifetime Qt Champion

    You can inspiration from e.g. "here":

    What issues are you facing ?

  • Just 2 issues:

    1. The QMessageBox is not displaying the concatenated username or the password.

    2. My randomizer is not generating random 5 character passwords from the concatenated first name and surname.

    I will look at the link as well.


  • Lifetime Qt Champion

    What are you currently getting in your message box ?

  • Nothing at all. It just pops up when I press OK and shows the username: and password: but with no data.

  • Lifetime Qt Champion

    @ //First letter of username to uppercase
    userName = " "; << You're a replacing userName content with a space
    userName.replace(0, 1, userName[0].toUpper());

  • Thanks, that fixed the username display issue.

    When I enter the fullname of john black , I get Johnb instead of Blacj

  • Lifetime Qt Champion

    Like I said, using only one input your user can put their information in the order they want so first name/last name or last name/first name. You really should write a little custom dialog that uses two QLineEdit for that. It will be clearer for your users

  • SGalst thanks for all the assistance. I have solved the issues.

Log in to reply