Qt:如何移动textit光标到特定的冷和行
Qt: how to move textEdit cursor to specific col and row
我可以通过QTextCursor::blockNumber()和QTextCursor::positionInBlock()获得行和col的当前位置。我的问题是,如何移动光标到特定的位置与行和col.如
setPosition(x,y) // The current cursor would move to row x and col y.
这可能吗?
简单的解决方案:
把光标移到这里:
textEdit.moveCursor(QTextCursor::Start); //set to start
for( <... y-times ...> )
{
textEdit.moveCursor(QTextCursor::Down); //move down
}
for( < ... x-times ...>)
{
textEdit.moveCursor(QTextCursor::Right); //move right
}
如果你需要"选择"文本来改变它,moveCursor也是goto-way。没有循环的类似方法也在最后。
更多的解释,也许更好的解决方案:
理论上,文本没有像gui那样的"行",但endline-character
( n
或rn
取决于操作系统和框架)只是另一个字符。所以对于一个光标来说,几乎所有的东西都只是一个没有行的"文本"。
有包装器函数来处理这个问题,但我稍后再讨论它们。首先,你不能通过QTextEdit
接口直接访问它们,但是你必须直接操作光标。
QTextCursor curs = textEdit.textCursor(); //copies current cursor
//... cursor operations
textEdit.setTextCursor(curs);
现在对于"操作":
如果你知道在字符串的哪个位置你有setPosition()
在这里。这个"位置"不是关于竖线的,而是关于整个文本的。
这是多行字符串内部的样子:
"Hello, World!nAnotherLine"
显示
Hello, World!
AnotherLine
setPosition()
需要内部字符串的位置。
要移动到另一行,您必须通过查找文本中的第一个n
并添加x偏移量来计算位置。如果您想要第三行查找前2个n
等
幸运的是,还有一个函数setVerticalMovement
,它似乎包装了这个,也许是你想做的。它垂直移动光标。
你可以这样写:
curs.setPosition(x); //beginning at first line
curs.setVerticalMovement(y); //move down to the line you want.
然后像上面那样用光标调用setTextCursor
。
注意:
顺序很重要。setPosition
设置整个文本中的位置。因此,setPosition(5)
虽然可能在第三行将而不是将其设置为整个文本的行中的第5个字符。所以先移动x坐标,再移动y。
你需要注意行的长度。
some longer line
short
another longer line
如果你现在指定第2行和第7列,它将是"out- bounds"。我不确定verticalMovement
在这里的行为。我假定光标位于行尾。
当你直接使用QTextCursor
类时,你也可以使用没有循环的移动操作,因为它们有一个额外的参数来重复操作。
curs.movePosition(QTextCursor::Start);
curs.movePosition(QTextCursor::Down,<modeMode>,y); //go down y-times
curs.movePosition(QTextCursor::Right,<moveMode>,x); //go right x-times
我认为最好的方法是通过QTextCursor
。
例如,如果您的QTextEdit
被称为textEdit
:
QTextCursor textCursor = ui->textEdit->textCursor();
textCursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, x);
textCursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, y);
ui->textEdit->setTextCursor(textCursor);
其中x
和y
为所需位置
如果文档布局使用每行1块(例如,它是QPlainTextEdit
或文档包含非常基本格式的文本),那么您可以直接设置位置:
void setCursorLineAndColumn (QTextCursor &cursor, int line, int col,
QTextCursor::MoveMode mode)
{
QTextBlock b = cursor.document()->findBlockByLineNumber(line);
cursor.setPosition(b.position() + col, mode);
// you could make it a one-liner if you really want to, i guess.
}
与向下+右方法相比,它的主要优点是在两个位置之间选择文本要简单得多:
QTextCursor cursor = textEdit->textCursor();
setCursorLineAndColumn(cursor, startLine, startCol, QTextCursor::MoveAnchor);
setCursorLineAndColumn(cursor, endLine, endCol, QTextCursor::KeepAnchor);
textEdit->setTextCursor(cursor);
- 如何在Qt C++中更改光标
- Win32 API 控制台光标在 WriteConsole 后不移动
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何更改输出的位置或光标在输出处,以便在"Code Blocks"中C++
- 使用 C++将文本光标移动到屏幕中不可见的行
- 在 C# 中更改鼠标光标图标
- ASCII 旋转光标 (TUI) 动画出现问题
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- 将光标单独移动到当前位置
- 如何获得鼠标光标连接时按下左键单击和在C++中释放左键单击时?
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- 如何在C++中模拟鼠标光标的移动
- 如何确定鼠标光标所在的控制台字符
- 如何在C++中使用光标定位
- 如何在QML中设置鼠标光标位置
- 如果传递到QGraphicsRectItem Qt c++,请更改光标
- 从 QML 更好地控制文本光标
- Windows 函数"GetCursorInfo"返回成功,但光标的句柄为 NULL
- 避免在光标移到子窗口上时WM_MOUSELEAVE
- Qt:如何移动textit光标到特定的冷和行