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. Prevent Enter key from clear my text in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Prevent Enter key from clear my text in QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.5k Views 2 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I have a QLineEdit where user will enter numbers and I will have a slot to catch the editingFinished signal when something has been entered. I want the number to stay and prevent user from clearing the text box on pressing the Enter key.

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

      Hi
      So when user press enter he is not allowed to edit the text anymore?
      You could just set it to ReadOnly.

      L 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        So when user press enter he is not allowed to edit the text anymore?
        You could just set it to ReadOnly.

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @mrjj They can edit, I just don't want to the text to disappear after enter.

        mrjjM 1 Reply Last reply
        0
        • L lansing

          @mrjj They can edit, I just don't want to the text to disappear after enter.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @lansing
          But i just tested one QPlainEdit.LineEdit
          Mine does not clear the text pressing enter.. ?

          L 1 Reply Last reply
          0
          • mrjjM mrjj

            @lansing
            But i just tested one QPlainEdit.LineEdit
            Mine does not clear the text pressing enter.. ?

            L Offline
            L Offline
            lansing
            wrote on last edited by
            #5

            @mrjj said in Prevent Enter key from clear my text in QLineEdit:

            QPlainEdit

            I'm using QLineEdit, not QPlainEdit. I have set a QDoubleValidator to limit the range to 0-120. When I enter 150 and pressed enter, the text remain, but when I entered 10, the text was cleared.

            mrjjM 1 Reply Last reply
            0
            • L lansing

              @mrjj said in Prevent Enter key from clear my text in QLineEdit:

              QPlainEdit

              I'm using QLineEdit, not QPlainEdit. I have set a QDoubleValidator to limit the range to 0-120. When I enter 150 and pressed enter, the text remain, but when I entered 10, the text was cleared.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Yep my bad i did mean LineEdit.

              Can you show how you set it up ?

              update:
              i tried with
              QDoubleValidator *myDblVal = new QDoubleValidator(0, 120, 2, this);
              ui->lineEdit->setValidator(myDblVal);
              and i cant get it to clear the text so not sure why yours does.

              L 1 Reply Last reply
              2
              • mrjjM mrjj

                Yep my bad i did mean LineEdit.

                Can you show how you set it up ?

                update:
                i tried with
                QDoubleValidator *myDblVal = new QDoubleValidator(0, 120, 2, this);
                ui->lineEdit->setValidator(myDblVal);
                and i cant get it to clear the text so not sure why yours does.

                L Offline
                L Offline
                lansing
                wrote on last edited by lansing
                #7

                @mrjj

                I have set the lineEdit default to readonly in the ui, and unlock it only when mode is equal to 1 set somewhere in the file.

                // at the top
                connect(m_ui.myLineEdit, &QLineEdit::editingFinished,
                        this, &MyDiag::slotSetNumber);
                
                // somewhere below
                void MyDiag::slotSetNumber() {
                    if (!m_ui.myLineEdit->isReadOnly()) {    
                        m_ui.myLineEdit->setReadOnly(true);
                    }
                
                    if (mode === 1) {
                        m_ui.myLineEdit->setReadOnly(false);
                        numText = QVariant(30).toString();
                    }
                
                    m_ui.myLineEdit->setText(numText);
                }
                
                mrjjM 1 Reply Last reply
                0
                • L lansing

                  @mrjj

                  I have set the lineEdit default to readonly in the ui, and unlock it only when mode is equal to 1 set somewhere in the file.

                  // at the top
                  connect(m_ui.myLineEdit, &QLineEdit::editingFinished,
                          this, &MyDiag::slotSetNumber);
                  
                  // somewhere below
                  void MyDiag::slotSetNumber() {
                      if (!m_ui.myLineEdit->isReadOnly()) {    
                          m_ui.myLineEdit->setReadOnly(true);
                      }
                  
                      if (mode === 1) {
                          m_ui.myLineEdit->setReadOnly(false);
                          numText = QVariant(30).toString();
                      }
                  
                      m_ui.myLineEdit->setText(numText);
                  }
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @lansing

                  Hi
                  I was wondering if mode is not 1 then
                  you call
                  m_ui.myLineEdit->setText(numText);
                  anyway and if numText is empty at that point in time, the lineEdit will be empty.

                  Could it be something like that `?

                  L 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @lansing

                    Hi
                    I was wondering if mode is not 1 then
                    you call
                    m_ui.myLineEdit->setText(numText);
                    anyway and if numText is empty at that point in time, the lineEdit will be empty.

                    Could it be something like that `?

                    L Offline
                    L Offline
                    lansing
                    wrote on last edited by lansing
                    #9

                    @mrjj

                    I edited the first if statement on last post, it should be if (!something) with the "!" in front.

                    The mode if statement does passed because I won't be able to type into it if it's false.

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

                      Hi
                      But are you sure
                      m_ui.myLineEdit->setText(numText);
                      not sets empty text ?

                      You could try

                      if (!numText.isEmpty())
                      m_ui.myLineEdit->setText(numText);

                      and see. else i have no guesses on why text is cleared.

                      L 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        Hi
                        But are you sure
                        m_ui.myLineEdit->setText(numText);
                        not sets empty text ?

                        You could try

                        if (!numText.isEmpty())
                        m_ui.myLineEdit->setText(numText);

                        and see. else i have no guesses on why text is cleared.

                        L Offline
                        L Offline
                        lansing
                        wrote on last edited by
                        #11

                        @mrjj

                        Oh you're right, I just double checked, the numText line was wrote outside the mode if statement, moving it back inside the if statement solved it.

                        1 Reply Last reply
                        2

                        • Login

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