Search code examples
pythonqt4pyqt4

Insert table into QTextEdit with PyQt4


How can I insert a tabel inside a QTextEdit to be printed on A4 paper. I wrote this code but I don't know how I can insert it into the value, just insert first cell:

self.text = QtGui.QTextEdit()
self.cursor = QtGui.QTextCursor()
self.cursor = self.text.textCursor()
self.cursor.insertTable(2, 5)
self.cursor.insertText("first cell ")

Solution

  • You need to move the position of the QTextCursor. Take a look at QTextCursor.movePosition and the operations in QTextCursor.MoveOperation.

    This should do the job for you:

    self.cursor.movePosition(QTextCursor.NextCell)
    self.cursor.insertText("second cell")