在Qt Creator的Gotoline

那么,我正在做一个转线系统。 但它似乎不起作用。 它之前做过,但我想我打破了它。

void ScriptWindow::gotoLine() { int line = QInputDialog::getInteger(myEdit, "Line Number","To what line do you want to go?", 1, 1, myEdit->document()->lineCount()); QTextCursor cursor = myEdit->textCursor(); myEdit->setTextCursor(cursor); cursor.setPosition(QTextCursor::Start, QTextCursor::MoveAnchor); while(cursor.position() == QTextCursor::Start) { cursor.setPosition(line - 1, QTextCursor::MoveAnchor); } } 

你能告诉我我做错了什么吗?

Solutions Collecting From Web of "在Qt Creator的Gotoline"

将光标位置设置为零,按行数向下移动,然后设置myEdit的文本光标。

 QTextCursor cursor = myEdit->textCursor(); cursor.setPosition(0); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, line-1); myEdit->setTextCursor(cursor); 

或者,通过QTextDocument找到位置,然后设置位置。

 int pos = myEdit->document()->findBlockByLineNumber(line-1).position(); QTextCursor cursor = myEdit->textCursor(); cursor.setPosition(pos); myEdit->setTextCursor(cursor);