【问题标题】:Unwanted Padding in QTextEdit HTML Subset <img>QTextEdit HTML 子集中不需要的填充 <img>
【发布时间】:2018-02-17 14:39:16
【问题描述】:

我正在尝试在 QTextEdit 子集中的表格 html 中插入图像。 我希望该图像与表格宽度完美契合。 不幸的是,Qt 没有什么能顺利进行,并且在图像的右侧和下方留下了令人讨厌的填充。

这是简化的代码(使用任何图像进行测试),有人知道如何避免它吗?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QCoreApplication, QRect, Qt

class Labhtml(QTextEdit):

    def __init__(self):
        super().__init__()

        html= '''
            <table border="1" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <img src="bar.png">
                </td>
            </tr>
            </table>
        '''

        self.setText(html)


class Example(QScrollArea):
    def __init__(self):
        super().__init__()

        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setAlignment(Qt.AlignTop)

        layout.addWidget(Labhtml())

        self.setWidget(widget)
        self.setWidgetResizable(True)

        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

当涉及到我几乎要放弃的子集时,Qt 充满了这些故障。

【问题讨论】:

    标签: html image html-table pyqt padding


    【解决方案1】:

    问题是由您在td 标记中添加的空格引起的。所以解决问题的一种方法是这样的:

    <table border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td><img src="image.png"></td>
    </tr>
    </table>
    

    或者,使用小于图像的固定尺寸:

    <table border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td width="0" height="0">
            <img src="image.png">
        </td>
    </tr>
    </table>
    

    【讨论】:

    • @user3755529。不要太难:) Qt 的简化 html 有它的局限性,所以需要一些时间来适应。你可能会发现有时你需要处理一些事情来得到你想要的东西。好处是它比成熟的 html 浏览器快方式
    猜你喜欢
    • 2017-05-03
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多