methodstooltipthumbnailspyside2qtextbrowser

In PySside2, how can I show the thumbnail for an item in QtextBrowser when the user hovers it?


Now I have a plan. That is dragging files to QttextBrowser.

When the mouse hovers over the path URL, the thumbnail will display to the right in QttextBrowser.

But even after consulting two Stack Overflow questions, I couldn’t achieve it:

How can I implement the plan? Are the keywords tooltip?

import sys
from pathlib import Path
from PySide2 import QtCore,QtUiTools
# import QtWidget Modules
from PySide2.QtWidgets import QApplication, QWidget, QLabel, QToolTip
# import QtGui modules
from PySide2.QtGui import QIcon, QFont
class UiLoader(QtUiTools.QUiLoader):
    _baseinstance = None

    def createWidget(self, classname, parent=None, name=''):
        if parent is None and self._baseinstance is not None:
            widget = self._baseinstance
        else:
            widget = super(UiLoader, self).createWidget(classname, parent, name)
            if self._baseinstance is not None:
                setattr(self._baseinstance, name, widget)
        return widget
    def loadUi(self, uifile, baseinstance=None):
        self._baseinstance = baseinstance
        widget = self.load(uifile)
        QtCore.QMetaObject.connectSlotsByName(widget)
        return widget
class MainWindow(QWidget):
    def __init__(self,parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui()

    def ui(self):
        self.test= UiLoader().loadUi('dropURL.ui', self)  
        self.setAcceptDrops(True)
        self.setToolTip('goods.png')

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()
    def dropEvent(self, event):

        urls = event.mimeData().urls()
        paths = [Path(url.toLocalFile()) for url in urls]
        self.textBrowser.setText('\n'.join([str(p) for p in paths]))
        self.textBrowser.setText.setToolTip("Active Icon")
    def setIconModes(self):
        # set icon
        icon1 = QIcon("geeksforgeeks.png")
        # set label
        label1 = QLabel('Sample', self)
        # set image in Active state
        pixmap1 = icon1.pixmap(100, 100, QIcon.Active, QIcon.On)
        # set Pixmap
        label1.setPixmap(pixmap1)
        # set tooltip text
        label1.setToolTip("Active Icon")
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()

Here is my code and UI profile: https://github.com/cj044/file-preview-plan

Currently, the picture is my plan to achieve it.

To_show_thumbnail


Solution

  • QTextCharFormat supports tooltips, and just like all tooltips in Qt they have support for basic HTML.

    As long as the image is a local file, you can just use the path in a standard <img> tag.

        def dropEvent(self, event):
            cursor = self.textBrowser.textCursor()
            for url in event.mimeData().urls():
                pos = cursor.position()
                path = url.toLocalFile()
                cursor.insertText(path)
                cursor.setPosition(pos, cursor.KeepAnchor)
                fmt = cursor.charFormat()
                fmt.setToolTip('<img src="{}">'.format(path))
                cursor.setCharFormat(fmt)
                cursor.movePosition(cursor.End)
                cursor.insertText('\n')