[Qt-interest] Implement a Go to line

David Villalobos Cambronero david.villalobos.c at gmail.com
Tue Apr 19 20:42:08 CEST 2011


Hi all, just to share my solutions with the list.

void Editor::gotoline(unsigned int lineNumber)
{
  const QTextBlock &block =
textEditor->document()->findBlockByNumber(lineNumber);
  QTextCursor cursor(block);
  cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 0);
  textEditor->setTextCursor(cursor);
  textEditor->centerCursor();
  textEditor->setFocus();
}

void Editor::itemClickedSlot(QListWidgetItem *item)
{
  gotoline((int)(symbolsLineNumber.value(item->text())));
}

Regards
---
David


On Tue, Apr 19, 2011 at 10:41, John Weeks <john at wavemetrics.com> wrote:

>
> On Apr 18, 2011, at 5:42 PM, David Villalobos Cambronero wrote:
>
> It does no work, the current line does not change. I use this code.
>
> void Editor::gotoline(int lineNumber)
> {
>   QTextCursor *cursor = new QTextCursor(textEditor->textCursor());
>   cursor->movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor,
> lineNumber);
> //  cursor.setPosition(lineNumber);
> }
>
> void Editor::itemClickedSlot(QListWidgetItem *item)
> {
>   gotoline((int)(symbolsLineNumber.value(item->text())));
> }
>
>
> As Ben said, don't create a new cursor, retrieve the cursor from the Text
> Edit widget. You can modify the text using a new cursor, but to change the
> display you need to use the cursor that's used by the widget.
>
> And you'll need to implement the part of my code that computes the
> difference between the current position and the desired line and use that
> offset rather than the line number itself.
>
>  Regards, John Weeks
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110419/f23fd12f/attachment.html 


More information about the Qt-interest-old mailing list