[Qt-interest] Implement a Go to line

BRM bm_witness at yahoo.com
Tue Apr 19 15:40:11 CEST 2011


Your problem is probably the first line of the gotoline() - don't create a new 
QTextCursor, just retrieve it.

QTextCursor* cursor = textEditor->textCursor();

Otherwise you are creating an entirely new cursor and modifying that, not the 
cursor used by the QTextEditor.

Ben


From: David Villalobos Cambronero <david.villalobos.c at gmail.com>
>
>To: qt-interest at trolltech.com
>Sent: Mon, April 18, 2011 8:42:12 PM
>Subject: Re: [Qt-interest] Implement a Go to line
>
>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())));
>}
>
>
>
>Regards
>---
>David
>
>
>
>On Mon, Apr 18, 2011 at 17:11, John Weeks <john at wavemetrics.com> wrote:
>
>
>>
>>On Apr 18, 2011, at 3:38 PM, David Villalobos Cambronero wrote:
>>
>>I have a QPlainTextEdit and I want to implement a Go To Line function, I've try 
>>to look in the source if qtcreator, but couldn't find any clue.
>>>
>>>Any idea of how to implement it?
>>>
>
>>QPlainTextEdit * edit = ...
>>QTextCursor cursor = edit->textCursor();
>>int linenumber = cursor->blockNumber();
>>int linedif = goto - linenumber;
>>if (linedif < 
>>0)cursor->movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 
>>-linedif);
>>else
>>
>>cursor->movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 
linedif);
>>
>>
>>
>>
>>That's uncompile pseudo-code :)
>>
>>
>>Seems complicated, eh? That's how I figured out to do it. Maybe there's some 
>>shortcut...
>>
>>Regards, John Weeks 
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110419/ffe8192b/attachment.html 


More information about the Qt-interest-old mailing list