The following code should work reliable:
```
void MarkdownEditor::gotoLine(int line)
{
    QTextCursor cursor(document()->findBlockByNumber(line-1));
    setTextCursor(cursor);
}
```