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. Using QTextCursor::setPosition() and bring to top of QTextEdit
Forum Update on Monday, May 27th 2025

Using QTextCursor::setPosition() and bring to top of QTextEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 10.8k 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    The way I have things right now, I have a QComboBox that contains key words that are parsed when text is loaded into a QTextEdit. When the user changes the combo box index, then I use a QTextCursor to search the document and jump to the spot in the QTextEdit (kind of acts as a quick search for the keywords).

    My one problem is that my search works and jumps to the keyword as well as highlights it, which is great, but the found keyword text is located on the bottom line of the QTextEdit. The functionality I would like is if it is found, then I want the keyword to be the first line of the text edit instead of the last line of the text edit.

    My only thought is to somehow get the amount of lines there are in the text edit and add on to the found text or something of that nature, but I figured I would ask here since there is probably an easier way to do this.

    This is the slot that gets called when the combo box index is changed:
    @
    /*
    // quick search index changed (private slot)
    */
    void MainWindow::quickSearchIndexChanged(QString text)
    {
    // error check text
    if (text == "")
    return;

    // error check index
    if (ui->combo_quick_search->currentIndex() < 0)
        return;
    
    // create / set text cursor
    QTextCursor fileViewerCursor;
    ui->textedit_output->setTextCursor(fileViewerCursor);
    
    // get find position
    int pos = ui->textedit_output->find(text);
    
    // set the text cursor position
    fileViewerCursor.setPosition(pos);
    

    }
    @

    Thank you in advance!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

      Does anyone have any suggestions? (bumping, apologize for double post).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        doc_symbiosis
        wrote on last edited by
        #3

        I think, you've got to set the setTextCursor to the end of the code snippet, means after the setposition.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          It might be worth a try to get the rectangle (in viewport coordinates) that includes the cursor using "QTextEdit::cursorRect() ":/doc/qt-4.8/qtextedit.html#cursorRect-2 and then ask the viewport to scroll to that position.

          http://www.catb.org/~esr/faqs/smart-questions.html

          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