Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Reading characters from QInputDialog and concatenating/append
Forum Updated to NodeBB v4.3 + New Features

[Solved] Reading characters from QInputDialog and concatenating/append

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aquanaut
    wrote on last edited by
    #1

    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

    blacj

    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).

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You're looking for "QString::left(int n)":http://doc.qt.io/qt-5/qstring.html#left

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aquanaut
        wrote on last edited by
        #3

        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

        blacj

        peter appendix

        appep

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

        Thanks

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aquanaut
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aquanaut
              wrote on last edited by
              #6

              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)];
              
                     do{
                           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;
              }
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aquanaut
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You can inspiration from e.g. "here":http://www.cplusplus.com/forum/windows/88843/

                  What issues are you facing ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aquanaut
                    wrote on last edited by
                    #9

                    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.

                    Thanks

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      What are you currently getting in your message box ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aquanaut
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            aquanaut
                            wrote on last edited by
                            #13

                            Thanks, that fixed the username display issue.

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

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                aquanaut
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved