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. How to find a line number after an Enter key press in QTextEdit.

How to find a line number after an Enter key press in QTextEdit.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 590 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.
  • J Offline
    J Offline
    jenya7
    wrote on 22 Nov 2021, 10:13 last edited by jenya7
    #1

    Say I have some lines in QTextEdit.

    Line number 1
    Line number 2
    Line number 3

    Now I press an Enter key after Line number 2 and it looks like this now

    Line number 1
    Line number 2
    '\r'
    Line number 3

    Now I wont to get a line before the Enter key - Line number 2

    So I can do like this

    switch (keyEvent->key())
     {
          case Qt::Key_Return:
            {
                 if (term_str.isEmpty())
                  {
                            QTextDocument *doc = ui->textEditTerminalTx->document();
                            QTextBlock tb = doc->findBlockByLineNumber(???);
                            QString s = tb.text();
                  }
         }
         break;
    }
    

    But how do I get a line number to put in doc->findBlockByLineNumber(???) ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 22 Nov 2021, 10:50 last edited by mpergand
      #2

      To retrieve the block under the cursor, you can do:

      QTextDocument *doc = ui->textEditTerminalTx->document();
      //QTextCursor cursor=doc->textCursor();
      QTextCursor cursor=ui->textEditTerminalTx->textCursor();
      QTextBlock tb = cursor.block();
      
      J 1 Reply Last reply 22 Nov 2021, 10:54
      0
      • M mpergand
        22 Nov 2021, 10:50

        To retrieve the block under the cursor, you can do:

        QTextDocument *doc = ui->textEditTerminalTx->document();
        //QTextCursor cursor=doc->textCursor();
        QTextCursor cursor=ui->textEditTerminalTx->textCursor();
        QTextBlock tb = cursor.block();
        
        J Offline
        J Offline
        jenya7
        wrote on 22 Nov 2021, 10:54 last edited by
        #3

        @mpergand said in How to find a line number after ann Enter key in QTextEdit.:

        To retrieve the block under the cursor, you can do:

        QTextDocument *doc = ui->textEditTerminalTx->document();
        QTextCursor cursor=doc->textCursor();
        QTextBlock tb = cursor.block();
        

        I need the line above the cursor - only one line above.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on 22 Nov 2021, 10:58 last edited by
          #4

          int QTextCursor::blockNumber() const
          Returns the number of the block the cursor is in, or 0 if the cursor is invalid.

          J 1 Reply Last reply 22 Nov 2021, 11:02
          0
          • M mpergand
            22 Nov 2021, 10:58

            int QTextCursor::blockNumber() const
            Returns the number of the block the cursor is in, or 0 if the cursor is invalid.

            J Offline
            J Offline
            jenya7
            wrote on 22 Nov 2021, 11:02 last edited by
            #5

            @mpergand
            QTextCursor cursor=doc->textCursor();
            I get - error: no member named 'textCursor' in 'QTextDocument'

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on 22 Nov 2021, 11:06 last edited by
              #6

              Sorry, it is:
              ui->textEditTerminalTx->textCursor();

              J 1 Reply Last reply 22 Nov 2021, 11:30
              0
              • V Offline
                V Offline
                VRonin
                wrote on 22 Nov 2021, 11:18 last edited by VRonin
                #7
                QTextCursor cursor(ui->textEditTerminalTx->document());
                const int oldPosition = cursor.position();
                cursor.movePosition(QTextCursor::Up);
                QString s = cursor.block().text();
                cursor.setPosition(oldPosition); //restore the cursor
                

                "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
                2
                • M mpergand
                  22 Nov 2021, 11:06

                  Sorry, it is:
                  ui->textEditTerminalTx->textCursor();

                  J Offline
                  J Offline
                  jenya7
                  wrote on 22 Nov 2021, 11:30 last edited by
                  #8

                  @mpergand
                  Thank you. This way it works good.

                  QTextCursor cursor= ui->textEditTerminalTx->textCursor();
                  QTextBlock tb = cursor.block();
                  QString line = tb.text();
                  

                  Always get the last line above the cursor. Exactly what I need.

                  1 Reply Last reply
                  1

                  4/8

                  22 Nov 2021, 10:58

                  • Login

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