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. QLineEdit accept comma delimited integers

QLineEdit accept comma delimited integers

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.4k Views 3 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.
  • Pl45m4P Pl45m4

    @SPlatten

    Use an inputMask, a QValidator or classic RegEx [but dont ask me how to do it with RegEx :) ]

    • https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop
    • https://doc.qt.io/qt-5/qvalidator.html

    Edit:

    To get range control (e.g., for an IP address) use masks together with validators.

    So you can use both to set your range

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #3

    @Pl45m4 , I'm trying:

    QString strRegEx = QString("^[%1-%2](,[%1-%2])*$").arg(strMin, strMax);
    QRegExp rx(strRegEx);
    QValidator* pValidator(new QRegExpValidator(rx,this));
    plnedInput->setInputMask("0,0");
    plnedInput->setValidator(pValidator);
    QObject::connect(plnedInput, &QLineEdit::editingFinished,
        [this, pValidator]() {
            delete pValidator;
        }
    );
    

    strRegEx for example could contain:

    ^[1-6](,[1-6])*$
    

    However with the validator set it will not accept anything. The requirement is for two numbers comma delimited being within the specified min and max range.

    I changed the expression to:

    ^[1-6],[1-6]$
    

    This works when tested on:
    http://softlion.com/webTools/RegExpTest/

    But in Qt I cannot enter anything.

    Kind Regards,
    Sy

    VRoninV 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @Pl45m4 , I'm trying:

      QString strRegEx = QString("^[%1-%2](,[%1-%2])*$").arg(strMin, strMax);
      QRegExp rx(strRegEx);
      QValidator* pValidator(new QRegExpValidator(rx,this));
      plnedInput->setInputMask("0,0");
      plnedInput->setValidator(pValidator);
      QObject::connect(plnedInput, &QLineEdit::editingFinished,
          [this, pValidator]() {
              delete pValidator;
          }
      );
      

      strRegEx for example could contain:

      ^[1-6](,[1-6])*$
      

      However with the validator set it will not accept anything. The requirement is for two numbers comma delimited being within the specified min and max range.

      I changed the expression to:

      ^[1-6],[1-6]$
      

      This works when tested on:
      http://softlion.com/webTools/RegExpTest/

      But in Qt I cannot enter anything.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #4

      @SPlatten said in QLineEdit accept comma delimited integers:

      (,%1

      You forgot a [

      plnedInput->setInputMask("0,0");

      Remove this, you have a validator

      QObject::connect(plnedInput, &QLineEdit::editingFinished, this, pValidator { delete pValidator; } );

      Why?! no, the parent will delete it

      This only works if strMin and strMax are signle digits

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        Hi,

        Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.

        Note that the way you do your ranges will only work for single digit values.

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

        SPlattenS 2 Replies Last reply
        3
        • SGaistS SGaist

          Hi,

          Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.

          Note that the way you do your ranges will only work for single digit values.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #6

          @SGaist, so to summarise, I should remove setInputMask ? And modify the regular expression to use QRegularExpressionValidatr ?

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • VRoninV VRonin

            @SPlatten said in QLineEdit accept comma delimited integers:

            (,%1

            You forgot a [

            plnedInput->setInputMask("0,0");

            Remove this, you have a validator

            QObject::connect(plnedInput, &QLineEdit::editingFinished, this, pValidator { delete pValidator; } );

            Why?! no, the parent will delete it

            This only works if strMin and strMax are signle digits

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #7

            @VRonin , yes you are right I did miss the [, however that was only me typing in the source incorrectly here as I couldn't copy and paste from the laptop I am using to code on.

            With regard to the comment on the connection to delete the validator, can you elaborate on what you mean by "the parent will delete it" ?

            Kind Regards,
            Sy

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

              Whatever class you use, you'll have to change your regular expression. In fact you'll have to build an expression generator to build your ranges.

              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
              • SPlattenS SPlatten

                @VRonin , yes you are right I did miss the [, however that was only me typing in the source incorrectly here as I couldn't copy and paste from the laptop I am using to code on.

                With regard to the comment on the connection to delete the validator, can you elaborate on what you mean by "the parent will delete it" ?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #9

                @SPlatten said in QLineEdit accept comma delimited integers:

                can you elaborate on what you mean by "the parent will delete it" ?

                You've set this as parent of pValidator. So it will be cleaned up when the parent (i.e. this) is deleted.
                Simple object-tree/ parent-child basics ;-)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.

                  Note that the way you do your ranges will only work for single digit values.

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #10

                  @SGaist , just tried using QRegularExpessionValidator, I'm using Qt 5.9.2, when I compile I get:

                  C2664:  'void QLineEdit::setValidator(const QValidator *)': cannot convert argument 1 from 'QRegularExpressionValidator' to 'const QValidator *'
                  

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @SPlatten said in QLineEdit accept comma delimited integers:

                    'QRegularExpressionValidator' to 'const QValidator *'

                    It's an object but needs a pointer - c++ basics.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    SPlattenS 1 Reply Last reply
                    4
                    • Christian EhrlicherC Christian Ehrlicher

                      @SPlatten said in QLineEdit accept comma delimited integers:

                      'QRegularExpressionValidator' to 'const QValidator *'

                      It's an object but needs a pointer - c++ basics.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #12

                      @Christian-Ehrlicher , thank you, sorry its very early here in the UK, brain hasn't woken up yet.

                      Kind Regards,
                      Sy

                      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