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

标签 python python-3.x pyqt pyqt5 qtextdocument

我试图使 QTextDocument 内的 QTextTable 具有文档的 100% 或全宽。但是 QTextTableFormat 类中没有方法可以将 QTextTable 格式化为 100% 宽度。我们可以调整行和列的大小,但不能调整整个表格的大小。

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

最佳答案

QTextTableFormat 继承自 QTextFrameFormat 它还有 setWidth() 允许您使用 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_())

enter image description here

关于python - 如何使 QTextTable 覆盖整个文档宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959410/

相关文章:

python - 当文本框具有焦点时,Windows 上 Qt 的 Ctrl+Alt 键修饰符行为

python - 如何使用另一台电脑的信息设置 QFileSystemModel?

python - 使用 matplotlib 在 wxpython 面板中组合按键和鼠标按钮事件

python - 如何在管理更新方法中从 Django 查询集中提取 id?

python - 执行用 yaml 编写的带有变量的 python 脚本

python - 'is' 运算符对 float 的表现出乎意料

python - 如何在 slider 中显示范围值

python - 如何像 bash 中的函数一样调用 python 脚本?

python - 如何使用 numpy 以行和列形式将数据保存在 .csv 文件中

python-3.x - 在多个列表中查找经常出现的单词