其他分享
首页 > 其他分享> > c-Qt Creator中的Gotoline

c-Qt Creator中的Gotoline

作者:互联网

好吧,我正在做一个Goto Line System.但似乎不起作用.以前做过,但是我想我把它弄坏了.

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);
    }    
}

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

解决方法:

将光标位置设置为零,向下移动行数,然后设置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);

标签:qt,qt-creator,nokia,c,windows
来源: https://codeday.me/bug/20191023/1913286.html