0

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

Good-boy
  • 3
  • 6
  • What do you mean by "I can't achieve it"? What did you try to get that? Can you show us a [mre] of those attempts? – musicamante Oct 29 '22 at 13:24
  • Thanks for your advise. I forgot post code. – Good-boy Oct 29 '22 at 14:30
  • Likely directly related to [this meta question](https://meta.stackexchange.com/questions/383404/). – Peter Mortensen Nov 01 '22 at 18:30
  • I post the same question on variety platform. Because they will give me variety answer., One answer will variety solution by everyone. And they are different direction for me. It is treasure for me. – Good-boy Nov 02 '22 at 07:18

1 Answers1

0

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')
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thanks for your advise. I keyed path in a standard tag. fmt.setToolTip('') . But it shows cute icon . It does not show thumbnail at right sight. – Good-boy Oct 31 '22 at 02:38
  • [link](https://i.imgur.com/ND4x8eY.png) According to the URL. https://stackoverflow.com/questions/45222252/what-is-the-correct-file-path-to-use-for-my-img-src – Good-boy Oct 31 '22 at 02:46
  • @Good-boy try `url.toString()` instead. – musicamante Oct 31 '22 at 03:36
  • I reference to your article [link](https://stackoverflow.com/questions/73667913/how-do-i-attach-a-hyperlink-to-the-highlighted-text-and-reset-the-cursor-propert) And I add url.toString() [link](https://imgur.com/KtmRiJk) [link](https://imgur.com/v8JzyHg) The situation is still shows cute icon not shows image. Perhaps it is mission impossible. I am a rookie in stackoverflow. Thanks for your advice. – Good-boy Oct 31 '22 at 08:57
  • SetToolTip is not support image? https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QToolTip.html#static-functions ,https://doc.qt.io/archives/qt-4.8/stylesheet-examples.html#customizing-qtooltip There is no image function. – Good-boy Nov 04 '22 at 13:07
  • @Good-boy It does work, otherwise I wouldn't have suggested it. As explained, you can only show images as embedded HTML images, not directly (that's why QToolTip only has functions that accept strings). Also, stylesheets are completely irrelevant for this: they are for *styling*, not for content. The problem is most certainly in the path. I just noticed that in your first comment you didn't use the quotes for the `src` value, as opposed to my code. Values of html tag attributes must always have quotes when their value can contain spaces, which is probably your case: `''`. – musicamante Nov 04 '22 at 15:41
  • I have ever looked at your solution in satackoverflow . You are professional in pyside2 etc. So I have lot of inquiry want ask you. Sorry I have no 50 reputation ,so I comment here. 1.According to your answer https://stackoverflow.com/questions/60521848/how-to-show-tooltip- The inquiry is asking pyqt5 but you reference to C++ classes? https://doc.qt.io/qt-5/qtextdocument.html – Good-boy Nov 05 '22 at 12:58
  • 2.Do I use another object to decrease difficult and code? for example use the object: https://i.stack.imgur.com/szgvn.jpg 3.In my question Line Edit ,Text Browser, List Widget are suitable. How and what to choose the object never change instead? Assume you are are ok. I could create new one question , welcome you to explain your experience. Thanks. – Good-boy Nov 05 '22 at 12:58
  • The '' is use in one image not for folder? Even the code does not use loop to scan total path of folder? – Good-boy Nov 05 '22 at 13:33
  • @Good-boy Sorry but I don't really understand your questions. 1. All Qt classes are C++ classes, PyQt (and PySide) are *python bindings*. 2+3. I don't know what you mean. Finally, I sincerely hope that you didn't *literally* use `` with the 3 dots, because that was clearly an example, and you **obviously** had to place the path of the image in place of the dots. – musicamante Nov 05 '22 at 13:52
  • Excuse me, I Embaded HTML img Tag attributes in setToolTip. When I using single path like below: It is OK. https://imgur.com/ye3Idmc I want to preview total image in folder. But I Changed to img Tag attributes like below: https://imgur.com/tAZUIYm img src="img/{{src}}.jpg" I use three URLs but it is just only shows the GOOD.jpg . https://imgur.com/K2EIvDj I just want the tooltip to show all of folder image with URL So I have to create new one question? – Good-boy Nov 06 '22 at 13:21
  • I know what you want to do, so please stop repeating it. Instead, be more careful in doing what is being asked, use the proper syntax, and **think** about what you're doing. In your second link the first url is wrong (there's a space after `C:/`), and the other commented lines are just *wrong*: 1. using double curly braces (`{{src}}`) *escapes* them, so they are completely ignored by `format()`; 2. adding a slash before and after the path doesn't make any sense; 3. you obviously can NOT use an asterisk as file path. – musicamante Nov 06 '22 at 14:45
  • The third link does not make any sense as well. Every time you call `setToolTip()` you *overwrite* the tool tip, so it obviously won't show the three images. Besides, this doesn't make sense with what you asked: you asked to show the image as tooltip "When the mouse hovers over the path URL". So, obviously, you need to set a specific tooltip for each url, which is exactly what I did in my example. And I sincerely hope that you don't really want to show **all** images in the *same* tool tip, because that would be just be a terrible idea. – musicamante Nov 06 '22 at 14:52
  • OK Thanks to your suggetion. You are master in python. I according to your solution in stackoverflows. https://reurl.cc/dexry8 You have ever say ....based on QTextDocument, don't support animations. So if I want to create a priview model. I change to use https://imgur.com/rSj6wd4 https://i.stack.imgur.com/szgvn.jpg the object. It is much easier than tooltips? Is your say terrible like the quetion? One path URL ONE sample code will like below: https://reurl.cc/dexry8 class ToolTipAnimation(QtWidgets.QLabel): Thank you. – Good-boy Nov 07 '22 at 07:18
  • Perhaps it will an useful method ? https://reurl.cc/eWmOax – Good-boy Nov 07 '22 at 12:39
  • I can't tell you what is good or not for your program, as only you know what you want to do: that's your decision. Asking if you should make your program do X or Y is not in the scope of StackOverflow (so, if you were thinking about creating a question for it, don't bother, as it will be closed). We can only answer questions to specific problems: you asked how to show an image as tooltip when dropping it into a QTextEdit, and I answered that, but if you want to show a list of images, QTextEdit is certainly not a good choice: its purpose is to show *text*. Consider using QListWidget instead. – musicamante Nov 07 '22 at 18:40