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



  • 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!



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



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.