简体   繁体   中英

PyQt5: How to set the hint text/picture to items in QTreeWidget?

I would like to explain my question in the following picture.

在此处输入图片说明

In my App there is a QDockWidget , which contains a QTreeWidget . Some of the items in QTreeWidget have long text.

By running the App, only part of these long texts are shown.

I would like to ask, how to show a pop-up window, in which the full text of the item is shown. And, if it is not so difficult, how to show a small picture in this pop-up window? (As I could imagine, I drawn the pop-up window in the picture above. )

Here is the code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setWindowTitle("test")
        self.setFixedWidth(900)
        self.setFixedHeight(300)

        self.tabs = QTabWidget()
        self.setCentralWidget(self.tabs)

        self.initUI()
        self.show()

    def createTreeWidget(self):
        self.tree = QTreeWidget()
        self.tree.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        self.tree.setColumnCount(1)
        self.tree.setHeaderHidden(True)

        # add items
        self.t_A = QTreeWidgetItem(self.tree)
        self.t_A.setText(0, "A")

        # add items
        self.t_B = QTreeWidgetItem(self.tree);          self.t_B.setText(0, "B")
        self.t_B1 = QTreeWidgetItem(self.t_B) ;        self.t_B1.setText(0, "Long long long text: show hint text, show pic B1")
        self.t_B2 = QTreeWidgetItem(self.t_B) ;        self.t_B2.setText(0, "Long long long text: show hint text, show pic B2")
        self.t_B3 = QTreeWidgetItem(self.t_B) ;        self.t_B3.setText(0, "Long long long text: show hint text, show pic B3")
        self.t_B4 = QTreeWidgetItem(self.t_B) ;        self.t_B4.setText(0, "Long long long text: show hint text, show pic B4")
        self.t_B5 = QTreeWidgetItem(self.t_B) ;        self.t_B5.setText(0, "Long long long text: show hint text, show pic B5")
        self.t_B6 = QTreeWidgetItem(self.t_B) ;        self.t_B6.setText(0, "Long long long text: show hint text, show pic B6")
        self.t_B7 = QTreeWidgetItem(self.t_B) ;        self.t_B7.setText(0, "Long long long text: show hint text, show pic B7")
        self.t_B8 = QTreeWidgetItem(self.t_B) ;        self.t_B8.setText(0, "Long long long text: show hint text, show pic B8")
        self.t_B9 = QTreeWidgetItem(self.t_B) ;        self.t_B9.setText(0, "Long long long text: show hint text, show pic B9")

        # add items
        self.t_C = QTreeWidgetItem(self.tree) ; self.t_C.setText(0, "C")
        self.t_C1 = QTreeWidgetItem(self.t_C) ;  self.t_C1.setText(0, "C1")
        self.t_C2 = QTreeWidgetItem(self.t_C) ;  self.t_C2.setText(0, "C2")

        self.tree.expandAll()
        return self.tree


    def createDock(self):
        self.dock = QDockWidget("Dock", self)
        self.dock.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.dock.setAllowedAreas(Qt.LeftDockWidgetArea)
        self.dock.setWidget(self.createTreeWidget())

        return self.dock


    def initUI(self):
        self.addDockWidget(Qt.LeftDockWidgetArea, self.createDock())
        self.statusBar().showMessage('Message in statusbar.')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    sys.exit(app.exec_())

Thanks for the help!

You can use the function setToolTip with html formating.
Try something like :

self.t_B1.setToolTip(0, '<b>Long long long text: show hint text, show pic B1</b><br><img src="%s">' % filename)

for each QTreeWidgetItems.
It should perform what you want.

Hope this help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM