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] How to Restrict Unwanted Characters from String List Using QRegExp ?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to Restrict Unwanted Characters from String List Using QRegExp ?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 6.2k 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.
  • M Offline
    M Offline
    Macro
    wrote on 17 Feb 2013, 15:00 last edited by
    #1

    Hi...

    I am having a QLineEdit and a QStringList. My String List contains "AJ", "BK", "CL", "DM" strings in it. Now, i need to check for the first character of the String List items, if it Accepts then Allow it to enter in the QLineEdit else it should be Restricted, same way i need to check for the second character if it accepts then Allow it, else Restrict. Using QRegExp how can i make this?? I tried to include my StringList in my QRegExp. But no use... :(

    Thanks & Regards..

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on 17 Feb 2013, 15:52 last edited by
      #2

      Read about QRegExpValidator which you can set to QLineEdit.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on 17 Feb 2013, 16:26 last edited by
        #3

        I know abt QRegExpValidator and i know how to set it.... But for this case, i don't know how to set the validator for this.... Anyways, thanks for your suggestion...

        Thanks & Regards..

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hostel
          wrote on 17 Feb 2013, 17:57 last edited by
          #4

          So you want to allow user to input in QLineEdit only values like: “AJ”, “BK”, “CL”, “DM”?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Macro
            wrote on 17 Feb 2013, 18:10 last edited by
            #5

            Yes.. For the First Time the Line Edit should Accept only A, B, C & D.. it should Reject J, K, L & M and other Alphabets Like E, F, G, H, I, O, P, Q, R, S....Z

            Thanks & Regards...

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hostel
              wrote on 17 Feb 2013, 18:19 last edited by
              #6

              @
              QRegExpValidator* validator = new QRegExpValidator( QRegExp( "AJ|BK|CL|DM" ) );
              lineEdit->setValidator( validator );
              @

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Macro
                wrote on 17 Feb 2013, 22:00 last edited by
                #7

                Thanks for your Suggestion... but this will not work in my case... The string list values may contain different set of items.. First it may contain like which i had mentioned above. the next time it may contain a different set of string items... so what should i do in that case???

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  Hostel
                  wrote on 17 Feb 2013, 22:42 last edited by
                  #8

                  You should read a QStringList doc ;)
                  @
                  QStringList sl;
                  sl << "AJ" << "BK" << "CL" << "DM";

                  QRegExpValidator* validator = new QRegExpValidator( QRegExp( sl.join( "|" ) ) );
                  lineEdit->setValidator( validator );
                  

                  @

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    slinavipuz
                    wrote on 17 Feb 2013, 22:45 last edited by
                    #9

                    [quote author="Rochi" date="1361138440"]Thanks for your Suggestion... but this will not work in my case... The string list values may contain different set of items.. First it may contain like which i had mentioned above. the next time it may contain a different set of string items... so what should i do in that case??? [/quote]

                    Is there any rule that will tell you how that set will change?

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      ChrisW67
                      wrote on 17 Feb 2013, 22:53 last edited by
                      #10

                      Sounds like a job for a restricted QComboBox rather than a free text editor.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Macro
                        wrote on 18 Feb 2013, 05:08 last edited by
                        #11

                        [quote author="slinavipuz" date="1361141106"]
                        Is there any rule that will tell you how that set will change?[/quote]

                        Yes..... I have a method called stringListType() in that if i pass any of the StringList then it will take will take the corresponding StringList items......

                        EDIT : Then i have to restrict the characters which are not in the StringList Items.......

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Macro
                          wrote on 18 Feb 2013, 05:12 last edited by
                          #12

                          [quote author="ChrisW67" date="1361141581"]Sounds like a job for a restricted QComboBox rather than a free text editor.
                          [/quote]

                          Yup, but not completely... for a free text editior we can use a QCompleter for getting the Suggestions of the StringList Items. But in this case, i have to restrict the other characters except the characters of the StringList

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Macro
                            wrote on 18 Feb 2013, 08:50 last edited by
                            #13

                            Whooooo..... Finally i had solved it by myself..... I had pasted the code below.....

                            @QString regExpression = "(";
                            for (int i = 0; i < getStringList()->size(); ++i)
                            {
                            regExpression.append(getStringList()->at(i) + "|");
                            }

                            QRegExp rE(regExpression);
                            q_LineEdit->setValidator(new QRegExpValidator(rE, this));@

                            Thank you all for your suggestions........

                            Thanks & Regards..

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              Hostel
                              wrote on 18 Feb 2013, 09:14 last edited by
                              #14

                              Your for loop is equal to my suggestion:
                              @
                              sl.join( "|" );
                              @

                              1 Reply Last reply
                              0

                              1/14

                              17 Feb 2013, 15:00

                              • Login

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