Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to validate string in line edit

    General and Desktop
    3
    4
    2099
    Loading More Posts
    • 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.
    • I
      Indrajeet last edited by

      I want to check if the input string in line edit starts with only alphabet or number.

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        Many ways to do this. You can react to QLineEdit::textChanged() or use "QLineEdit::setValidator()":http://qt-project.org/doc/qt-4.8/qlineedit.html#setValidator for example.

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • B
          b1gsnak3 last edited by

          If you do not want to block the input so that he cannot write anything other than alphabet or number, you could check this with QLineEdit::editingFinished() and if the string is not valid display a message

          1 Reply Last reply Reply Quote 0
          • I
            Indrajeet last edited by

            Hi

            I have used setValidator in textChanged() of LineEdit but it is not working consistently.
            First time it works then if i switch to other window and come back then that line edit will allow all characters.

            ui.edit->setValidator(new QRegExpValidator(QRegExp("[a-z0-9]{1}[a-z0-9_-]{0,255}"),NULL));

            What is going wrong?

            1 Reply Last reply Reply Quote 0
            • First post
              Last post