当子集HTML QLabel时,我无法理解高度行为

时间:2017-09-08 05:16:33

标签: html pyqt height subset qlabel

当我在QWidget中放置带有html Subset的QLabel时,我会得到不同类型的行为,具体取决于QLabel是在自定义窗口小部件中还是在主窗口中或布局中。

主要问题是并非html表的所有行都显示出来。内容被砍成高度。

事实上,我发现HTML子集中不支持'height'属性很奇怪,它会让生活更轻松。

这是代码:

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

class Labhtml(QLabel):

    def __init__(self, parent):
        super().__init__(parent=parent)

        html = ('''
            <body>
            <table>
            <tr>
                <td id="header_cell" width="200">
                    <div id="ticker"><!--INST.T--></div>
                    <div id="price">   1000.25<!--price--></div>
                </td>
            </tr>

            <tr>
                </td>
                <td id="values_cells">
                    <div id="value">   +0.23%<!--value02--></div>
                    <div id="average"> 0.13%<!--range02--></div>
                </td>
            </tr>
            </table>
            </body>
            </html>

            ''')

        label = QLabel(html, self)
        self.setStyleSheet('QFrame {background-color: grey;}')

        self.resize(100, 150)


class Wid(QWidget):
    def __init__(self, parent):
        super().__init__(parent=parent)

        self.resize(100, 450)
        Labhtml(self)

        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignTop)

        layout.setSpacing(0)


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

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

        Labhtml(widget)
        layout.addWidget(Wid(widget))
        layout.addWidget(Labhtml(widget))


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

        self.show()


if __name__ == '__main__':

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

1 个答案:

答案 0 :(得分:1)

以下是您的示例的固定版本,似乎可以正常工作。希望你能看到我必须做出哪些改变:

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

class Labhtml(QLabel):
    def __init__(self, parent):
        super().__init__(parent=parent)
        html = ('''
            <body>
            <table>
            <tr>
                <td id="header_cell" width="200">
                    <div id="ticker"><!--INST.T--></div>
                    <div id="price">   1000.25<!--price--></div>
                </td>
            </tr>
            <tr>
                <td id="values_cells">
                    <div id="value">   +0.23%<!--value02--></div>
                    <div id="average"> 0.13%<!--range02--></div>
                </td>
            </tr>
            </table>
            </body>
            </html>
            ''')
        self.setText(html)
        self.setStyleSheet('QFrame {background-color: grey;}')

class Wid(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        label = Labhtml(self)
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setAlignment(Qt.AlignTop)
        layout.addWidget(label)
        layout.setSpacing(0)

class Example(QScrollArea):
    def __init__(self):
        super().__init__()
        widget = QWidget()
        layout = QVBoxLayout(widget)
        layout.setAlignment(Qt.AlignTop)
        layout.addWidget(Wid(widget))
        layout.addWidget(Labhtml(widget))
        self.setWidget(widget)
        self.setWidgetResizable(True)
        self.show()

if __name__ == '__main__':

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