在 PyQT 中为 QTreeView 项显示工具提示 [英] Displaying tooltips in PyQT for a QTreeView item

查看:80
本文介绍了在 PyQT 中为 QTreeView 项显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照 Yasin Uludag 的一些有用的在线教程尝试使用 PyQt(或者更确切地说是 PySide)来创建一个简单的树视图,但是我在让工具提示起作用时遇到了问题.在以下代码中,工具提示文本显示在控制台而不是工具提示窗口中.我见过的所有其他示例都直接在小部件项上使用 setToolTip,但我认为我无法在这种模型/视图方法中直接访问它.我需要对 QTreeView 本身进行一些初始化吗?

I have followed some useful online tutorials by Yasin Uludag to experiment with PyQt (or rather PySide) to create a simple tree view, but I'm having problems with getting tooltips to work. In the following code, the tooltip text is displayed on the console rather than in a tooltip window. All the other examples I have seen use setToolTip directly on the widget item, but I don't think I have direct access to that in this Model/View approach. Is there some initialization I need to do on the QTreeView itself?

 class TreeModel(QtCore.QAbstractItemModel):

     def __init__(self, root, parent=None):
         super(NXTreeModel, self).__init__(parent)
         self._rootNode = root

     def data(self, index, role):

          node = index.internalPointer()

         if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
             return node.name()

         if role == QtCore.Qt.ToolTipRole:
             return node.keys()

推荐答案

它像下面的代码一样工作.

It worked like below code.

class TreeModel(QAbstractItemModel):
    ...
    def data(self, index, role=Qt.DisplayRole):
        ...
        if role == Qt.ToolTipRole:
            return 'ToolTip'

    def flags(self, index):
        if not index.isValid():
            return Qt.NoItemFlags # 0
        return Qt.ItemIsSelectable # or Qt.ItemIsEnabled

这篇关于在 PyQT 中为 QTreeView 项显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆