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. Get the last two characters written in a QTextEdit
QtWS25 Last Chance

Get the last two characters written in a QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
qtextedithighlightselection
8 Posts 4 Posters 2.6k Views
  • 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
    legitnameyo
    wrote on last edited by
    #1

    I want to get the last two characters written in a QTextEdit. I tried the following

    void MainWindow::onTextChanged() {
    
        QTextCursor c = textEdit->textCursor();
        c.setPosition(textEdit->cursor().pos().x());
        c.setPosition(textEdit->cursor().pos().x() - 2, QTextCursor::KeepAnchor);
        textEdit->setTextCursor(c);
    
        qDebug() << textEdit->textCursor().selectedText();
    }
    

    Which partially works since I get the last two characters of the text but I also highlight them and rewrite them. I want it to be impossible for the user to see or know that the last two characters were retrieved and I also want to continue to write without overwriting the last two characters in my text!

    aha_1980A 1 Reply Last reply
    0
    • L legitnameyo

      I want to get the last two characters written in a QTextEdit. I tried the following

      void MainWindow::onTextChanged() {
      
          QTextCursor c = textEdit->textCursor();
          c.setPosition(textEdit->cursor().pos().x());
          c.setPosition(textEdit->cursor().pos().x() - 2, QTextCursor::KeepAnchor);
          textEdit->setTextCursor(c);
      
          qDebug() << textEdit->textCursor().selectedText();
      }
      

      Which partially works since I get the last two characters of the text but I also highlight them and rewrite them. I want it to be impossible for the user to see or know that the last two characters were retrieved and I also want to continue to write without overwriting the last two characters in my text!

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @legitnameyo

      Would it alread help to remove textEdit->setTextCursor(c); and instead use qDebug() << c.selectedText(); ?

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • L Offline
        L Offline
        legitnameyo
        wrote on last edited by
        #3

        Depending on how many characters I've got in my QTextEdit I get one of these two messages

        QTextCursor::setPosition: Position '458' out of range
        QTextCursor::setPosition: Position '460' out of range
        

        or if there is quite little text

        ""
        ""
        ""
        

        Removing "textEdit->setTextCursor(c);" fixed the issue of the QTextEdit highlighting and removing text but makes the printing of the last two characters impossible, AKA it gives one of the two errors I mentioned above.

        J.HilkJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          Edit: Wrong code, see below

          QTextCursor cur = textEdit->textCursor();
          newCur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor,2);
          qDebug() << textEdit->textCursor().selectedText();
          newCur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::MoveAnchor,2);
          

          "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

          1 Reply Last reply
          1
          • L Offline
            L Offline
            legitnameyo
            wrote on last edited by
            #5

            @VRonin I tried

            QTextCursor cur = textEdit->textCursor();
                cur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor,2);
                qDebug() << textEdit->textCursor().selectedText();
                cur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::MoveAnchor,2);
            

            but it gave me

            ""
            ""
            ""
            ""
            ""
            

            in the console, not the last two characters... It did solve the "out of range" issue though!

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              sorry, sloppy coding, the correct is below:

              QTextCursor cur = textEdit->textCursor();
              cur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor,2);
              qDebug() << cur.selectedText();
              

              "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

              1 Reply Last reply
              1
              • L Offline
                L Offline
                legitnameyo
                wrote on last edited by
                #7

                @VRonin it worked! thanks!

                void MainWindow::onTextChanged() {
                    QTextCursor cur = textEdit->textCursor();
                    cur.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor,2);
                    qDebug() << cur.selectedText();
                }
                
                1 Reply Last reply
                0
                • L legitnameyo

                  Depending on how many characters I've got in my QTextEdit I get one of these two messages

                  QTextCursor::setPosition: Position '458' out of range
                  QTextCursor::setPosition: Position '460' out of range
                  

                  or if there is quite little text

                  ""
                  ""
                  ""
                  

                  Removing "textEdit->setTextCursor(c);" fixed the issue of the QTextEdit highlighting and removing text but makes the printing of the last two characters impossible, AKA it gives one of the two errors I mentioned above.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @legitnameyo

                  QString str = textEdit->toPlainText();
                  QString targetChars = str.mid(textEdit->textCursor().columnNumber() -2 ,2);
                  qDebug() <<targetChars;
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  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