lavalade
lavalade

Reputation: 368

PyQt: QWhatsThis, display HTML page with image

I want to display a contextual help/doc with QWhatsThis from a HTML file with embeded image; but the image is not displayed.

According to the documentation QWhatsThis Class Reference, QWhatsThis allows rich-text input; rich-text is a subset of HTML4, the page from PyQt4 documentation richtext html subset in unavailable, but according to Qt5 documentation Supported HTML Subset, the tag:

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

is supported.

Here is the minimal working example I wrote :

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

and the HTML page with an image I want to display:

<!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>

I tried three different configuration:

and I get the exact same output, i.e. the image is not displayed:

Screenshot when I press Shift+f1 with cursor over the button

What is the problem? Does QWhatsThis really supports image? Is there something to add to the HTML file?

NB: The python source is the one for Python 2.7.5 and PyQt4; all the sources are available on Google drive.

Upvotes: 1

Views: 909

Answers (1)

eyllanesc
eyllanesc

Reputation: 244093

The problem is caused because the width attribute you don't have to point to "px" but only the value since the tooltip uses QTextDocument which only supports some HTML4 features. So you must change:

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

to

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

enter image description here

Upvotes: 1

Related Questions