Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. In einen QlineEdit die ersten 2 Buchstaben dürfen nicht gelöscht werden
Forum Updated to NodeBB v4.3 + New Features

In einen QlineEdit die ersten 2 Buchstaben dürfen nicht gelöscht werden

Scheduled Pinned Locked Moved Solved German
7 Posts 2 Posters 2.7k 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.
  • G Offline
    G Offline
    Galilio
    wrote on last edited by
    #1

    Hallo Zusammen,

    ich möchte in einen QlineEdit verhindert, dass die zwei ersten Buchtstaben nicht gelöscht werden können.
    Wie kann ich das tun?

    danke in voraus

    raven-worxR 1 Reply Last reply
    0
    • G Galilio

      Hallo Zusammen,

      ich möchte in einen QlineEdit verhindert, dass die zwei ersten Buchtstaben nicht gelöscht werden können.
      Wie kann ich das tun?

      danke in voraus

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Galilio

      QLineEdit* lineEdit = ....
          lineEdit->setText("XXX"); // text should always start with "XXX"
          lineEdit->setValidator( new QRegularExpressionValidator(QRegularExpression("^[X]{3}.*"), lineEdit ) ); // "XXX" followed by arbitrary characters
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • G Offline
        G Offline
        Galilio
        wrote on last edited by
        #3

        Danke
        und wenn ich Qt 4 habe?

        raven-worxR 1 Reply Last reply
        0
        • G Galilio

          Danke
          und wenn ich Qt 4 habe?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Galilio said in In einen QlineEdit die ersten 2 Buchstaben dürfen nicht gelöscht werden:

          und wenn ich Qt 4 habe?

          dann QRegExpValidator anstelle von QRegularExpressionValidator

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          G 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Galilio said in In einen QlineEdit die ersten 2 Buchstaben dürfen nicht gelöscht werden:

            und wenn ich Qt 4 habe?

            dann QRegExpValidator anstelle von QRegularExpressionValidator

            G Offline
            G Offline
            Galilio
            wrote on last edited by
            #5

            @raven-worx
            Ich kann aber trotzdem diese Buchstaben löschen und das will ich unbedingt verhindert.
            Diese Buchstaben varieren sich ja nachdem in welchen Status die SW sich befindet.

            raven-worxR 1 Reply Last reply
            0
            • G Galilio

              @raven-worx
              Ich kann aber trotzdem diese Buchstaben löschen und das will ich unbedingt verhindert.
              Diese Buchstaben varieren sich ja nachdem in welchen Status die SW sich befindet.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @Galilio
              Ah ja, habe vergessen, dass der Validator ja auch einen Intermediate state hat :/

              Versuch mal folgendes:

              class MyValidator : public QValidator
              {
                   Q_OBJECT
                   Q_PROPERTY( QString prefix READ prefix WRITE setPrefix )
              
              public:
                  MyValidator( const QString & prefix, QLineEdit* lineEdit )
                       : QValidator(lineEdit), m_LineEdit(lineEdit)
                  {
                         this->setPrefix(prefix);
                  }
              
                  virtual QValidator::State validate(QString &input, int &pos) const Q_DECL_OVERRIDE
                  {
                         return input.startsWith( m_Prefix ) ? QValidator::Acceptable : QValidator::Invalid;
                  }
              
                  QString prefix() const { 
                          return m_Prefix; 
                  }
                  void setPrefix(const QString & prefix) {
                          if( m_Prefix != prefix )
                          {
                                  // update old prefix in lineedit
                                  QString text = m_LineEdit->text();
                                  if( text.isEmpty() )
                                        m_LineEdit->setText( prefix );
                                  else
                                        m_LineEdit->setText( text.replace( 0, m_Prefix .length(), prefix ) );
              
                                  m_Prefix = prefix;
                                  emit changed();
                          }
                  }
              
              private:
                  QLineEdit* m_LineEdit;
                  QString m_Prefix;
              };
              

              Beispiel:

              QLineEdit* lineEdit = ....
                  lineEdit->setValidator( new MyValidator("XX", lineEdit ) );
              

              Wann immer sich jetzt der Prefix in der SW ändern sollte, ruft du einfach setPrefix("YY") auf, und das LineEdit widget sollte sich ebenfalls korrigieren.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • G Offline
                G Offline
                Galilio
                wrote on last edited by
                #7

                vielen Dank

                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