如何使 QTextTable 覆盖整个文档宽度

How to make QTextTable to cover whole document width

我正在尝试使 QTextDocument 中的 QTextTable 具有文档的 100% 或全宽。但是 QTextTableFormat class 中没有方法可以将 QTextTable 格式化为 100% 宽度。我们可以调整行和列的大小,但不能调整整体 table。

有什么办法可以实现吗,如果你知道,请分享。谢谢

因为QTextTableFormat inherits from QTextFrameFormat it also has the setWidth() method that allows you to set the width using QTextLength可以将宽度设置为文档宽度的百分比:

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_())