【问题标题】:PyQt: QWhatsThis, display HTML page with imagePyQt:QWhatsThis,显示带有图像的 HTML 页面
【发布时间】:2020-07-13 18:22:35
【问题描述】:

我想在 HTML 文件 中显示带有 QWhatsThis 的上下文帮助/文档,并嵌入 图像;但图像不显示。

根据文档 QWhatsThis Class Reference, QWhatsThis 允许富文本输入;富文本是 HTML4 的子集,来自 PyQt4 文档的页面 richtext html subset 不可用,但根据 Qt5 文档 Supported HTML Subset, 标签:

<img src="image.png" width="42px" height="314px"/>

支持。

这是我编写的最小工作示例:

# -*- coding: utf-8 -*-

from PyQt4.QtGui import QApplication, QPushButton, QMainWindow, QWidget, QHBoxLayout, QLabel
import sys

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.setWindowTitle("PyQt4 WhatsThis display image")
        self.setGeometry(500, 200, 300, 250)  # left, top, width, height

        hbox = QHBoxLayout()
        button = QPushButton("Click me", self)

        with open("doc.html", 'r') as fr:
            html_doc = fr.read().decode('utf8')
        button.setWhatsThis(html_doc)

        hbox.addWidget(button)
        self.setLayout(hbox)
        self.show()

app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())

以及带有我要显示的图像的 HTML 页面:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
        <h1>Repère pour les angles du champ de vitesse en entrée</h1>
        <p>Voilà de l’aide !</p>
        <img src="image.png" width="512px"/>
        <p>Bla bla bla</p>
    </body>
</html>

我尝试了三种不同的配置:

  • Python 2.7.5,PyQt4
  • Python 3.7.7,PyQt4
  • Python 3.7.7,PyQt5

我得到完全相同的输出,图像未显示:

当我按下 Shift+f1 并将光标悬停在按钮上时的屏幕截图

有什么问题? QWhatsThis 真的支持图片吗? 有什么要添加到 HTML 文件中的吗?

注意:python 源是 Python 2.7.5PyQt4 的源;所有资源都可以在Google drive 上找到。

【问题讨论】:

    标签: python html image pyqt


    【解决方案1】:

    这个问题是因为你不必指向“px”的宽度属性而只指向值,因为工具提示使用只支持some HTML4 features的QTextDocument。所以你必须改变:

    <img src="image.png" width="512px"/>
    

    <img src="image.png" width="512"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2012-12-13
      • 1970-01-01
      相关资源
      最近更新 更多