QTextTable 移动 QTextCursor 到一个确定的单元格
QTextTable move QTextCursor to a determinated Cell
我有一个 QTextTable
,我在 table 中做了一些操作,我想要两件事:
Return QTextCursor
到单元格 (0,0)
或移动QTextCursor
到单元格(行,列)
我该怎么办?
对于 QTextTable
类型的给定 table
对象,获取所需的游标:
auto tableCell = table->cellAt(row, column);
auto cursor = tableCell.firstCursorPosition();
然后 setTextCursor 在已编辑的 QTextEdit
对象中。
根据@ekhumoro 和@retmas 的建议,我这样做了:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QTextCursor
#### EKHUMORO suggestion
def move_cursor(table_text,cursor,row_destiny,col_destiny):
row=table_text.cellAt(cursor).row()
while not row==row_destiny:
if row<row_destiny:
cursor.movePosition(QTextCursor.NextRow)
else:
cursor.movePosition(QTextCursor.PreviousRow)
row=table_text.cellAt(cursor).row()
col=table_text.cellAt(cursor).column()
while not col==col_destiny:
if col<col_destiny:
cursor.movePosition(QTextCursor.NextCell)
else:
cursor.movePosition(QTextCursor.PreviousCell)
col=table_text.cellAt(cursor).column()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QTextEdit()
cursor=widget.textCursor()
table_text=cursor.insertTable(10,10)
# Format Table
fmt = table_text.format()
fmt.setWidth(QtGui.QTextLength(QtGui.QTextLength.PercentageLength, 100))
table_text.setFormat(fmt)
row=2
col=5
move_cursor(table_text,cursor,row,col)
cursor.insertText('Cell {} {}'.format(row,col))
#### RETMAS SUGGESTION
row=3
column=6
tableCell = table_text.cellAt(row, column)
cursor = tableCell.firstCursorPosition()
widget.setTextCursor(cursor)
cursor.insertText('Cell {} {}'.format(row,column))
widget.show()
sys.exit(app.exec_())
这很好用。
我有一个 QTextTable
,我在 table 中做了一些操作,我想要两件事:
Return
QTextCursor
到单元格 (0,0)或移动
QTextCursor
到单元格(行,列)
我该怎么办?
对于 QTextTable
类型的给定 table
对象,获取所需的游标:
auto tableCell = table->cellAt(row, column);
auto cursor = tableCell.firstCursorPosition();
然后 setTextCursor 在已编辑的 QTextEdit
对象中。
根据@ekhumoro 和@retmas 的建议,我这样做了:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QTextCursor
#### EKHUMORO suggestion
def move_cursor(table_text,cursor,row_destiny,col_destiny):
row=table_text.cellAt(cursor).row()
while not row==row_destiny:
if row<row_destiny:
cursor.movePosition(QTextCursor.NextRow)
else:
cursor.movePosition(QTextCursor.PreviousRow)
row=table_text.cellAt(cursor).row()
col=table_text.cellAt(cursor).column()
while not col==col_destiny:
if col<col_destiny:
cursor.movePosition(QTextCursor.NextCell)
else:
cursor.movePosition(QTextCursor.PreviousCell)
col=table_text.cellAt(cursor).column()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QTextEdit()
cursor=widget.textCursor()
table_text=cursor.insertTable(10,10)
# Format Table
fmt = table_text.format()
fmt.setWidth(QtGui.QTextLength(QtGui.QTextLength.PercentageLength, 100))
table_text.setFormat(fmt)
row=2
col=5
move_cursor(table_text,cursor,row,col)
cursor.insertText('Cell {} {}'.format(row,col))
#### RETMAS SUGGESTION
row=3
column=6
tableCell = table_text.cellAt(row, column)
cursor = tableCell.firstCursorPosition()
widget.setTextCursor(cursor)
cursor.insertText('Cell {} {}'.format(row,column))
widget.show()
sys.exit(app.exec_())
这很好用。