pythonpython-3.xpyqtpyqt5qtextdocument

How to make QTextTable to cover whole document width


I am trying to make the QTextTable inside QTextDocument to have 100% or full width of the document. But there is no method in QTextTableFormat class to format the QTextTable to have 100% width. We can resize row and column but not overall table.

Is there any way I can achieve that, if you know how, please share. Thanks


Solution

  • Since QTextTableFormat inherits from QTextFrameFormat it also has the setWidth() method that allows you to set the width using QTextLength as a basis that can set the width as a percentage of the document width:

    import sys
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QTextEdit()
        table = w.textCursor().insertTable(4, 5)
    
        fmt = table.format()
        fmt.setWidth(QtGui.QTextLength(QtGui.QTextLength.PercentageLength, 100))
        table.setFormat(fmt)
    
        w.show()
        sys.exit(app.exec_())
    

    enter image description here