【问题标题】:How to select a table using QTextCursor如何使用 QTextCursor 选择表格
【发布时间】:2014-04-24 08:43:13
【问题描述】:
我希望这段代码应该可以工作,但它没有
QTextCursor cursor = textEdit->textCursor();
cursor = QTextCursor(cursor.currentFrame());
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
textEdit->setTextCursor(cursor);
textEdit->copy(); // Here I got only text from current cell, not a table
【问题讨论】:
标签:
qt
qtextedit
qtextdocument
qtextcursor
【解决方案1】:
QTextEdit 或 QTextDocument 中的 QTextTable 单元格由 QTextBlock 本身表示。
您的示例代码确实将光标的位置移动到当前块的末尾,即单元格内容的末尾。
为了选择表格的全部内容,您需要选择所有单元格。
这应该可行:
cursor.movePosition( QTextCursor::Start);
while( cursor.movePosition( QTextCursor::NextCell, QTextCursor::KeepAnchor ) ){
//...add break condition as failsafe after n iterations?
}
请注意,您可以使用以下方式查询选择:
cursor.selectedText();