Qt Forum

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

    Solved Get the last two characters written in a QTextEdit

    General and Desktop
    qtextedit highlight selection
    4
    8
    1383
    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.
    • L
      legitnameyo last edited by

      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_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @legitnameyo last edited by

        @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 Reply Quote 1
        • L
          legitnameyo last edited by

          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.Hilk 1 Reply Last reply Reply Quote 0
          • V
            VRonin last edited by VRonin

            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 Reply Quote 1
            • L
              legitnameyo last edited by

              @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 Reply Quote 0
              • V
                VRonin last edited by VRonin

                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 Reply Quote 1
                • L
                  legitnameyo last edited by

                  @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 Reply Quote 0
                  • J.Hilk
                    J.Hilk Moderators @legitnameyo last edited by

                    @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

                    Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

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