[SOLVED] Scrolling to a particular line number in the QTextBrowser



  • Hi,
    Currently I have this code which goes to a particular line number and makes the text bold. What i want to achieve is that when the QTextBrowser is invoked, it will open to last position of the cursor below and not the start of the QTextBrowser(as default).

    Please help.

    @
    QTextDocument *document = my_browser->document();
    QTextCursor cursor(document);

    for(int i=0; i<20 ; i++)
    {
    cursor.movePosition(QTextCursor::Down);
    }
    cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    
    QTextCharFormat format;
    format.setFontWeight(QFont::Bold);
    
    cursor.mergeCharFormat(format);
    

    @



  • No replies...Someone please suggest me how to go about...



  • You can set position manually:
    @cursor.movePosition(QTextCursor::Start);@



  • You could use as alternative the select function of the QTextCursor:
    @
    cursor.select(QTextCursor::LineUnderCursor);
    cursor.mergeCharFormat(format);
    @



  • http://stackoverflow.com/questions/3547185/scroll-qtextbrowser-to-the-top
    This helped me. Thank you to everyone who tried to help me. I appreciate it.

    @
    QTextDocument *document = my_browser->document();
    QTextCursor cursor(document);

    for(int i=0; i<line_number-1 ; i++)
    {
    cursor.movePosition(QTextCursor::Down);
    }
    cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    
    my_browser->setTextCursor(cursor);
    mainLayout->addWidget(my_browser);
    setLayout(mainLayout);
    

    @


Log in to reply
 

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