PySide:将图像添加到工具提示 [英] PySide: Add images to tooltips

查看:57
本文介绍了PySide:将图像添加到工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个列出目录中文件的工具.当您将鼠标悬停在每个文件上时,它会显示一个工具提示,显示日期和制作文件的人.我还想要一个图像来伴随这些数据.

I'm building a tool that lists files in a directory. When you hover over each file it shows a tooltip displaying the date and who made the file. I'd also like an image to accompany this data.

是否可以在工具提示中插入图像?对于 QTreeView 中的每个项目,我希望弹出一个特定的图像和文本.如果不能通过工具提示完成,还有其他选择吗?

Is it possible to insert an image into a tooltip? For each item in the QTreeView I'd like a specific image and text to popup. If it can't be done with tooltips are there other alternatives?

from PySide import QtCore, QtGui
from shiboken import wrapInstance
import maya.OpenMayaUI as mui

def get_parent():
    ptr = mui.MQtUtil.mainWindow()
    return wrapInstance( long( ptr ), QtGui.QWidget )   

############################################        
''' Classes '''
############################################
class Main_Window( QtGui.QDialog ):
    def __init__( self, parent=get_parent() ):
        super( Main_Window, self ).__init__( parent )

        self.create_gui()
        self.create_layout()
        self.create_connections()
        self.get_contents()

    #--------------------------------------------------------------------
    def create_gui( self ):
        self.tv_model=MyModel()
        self.tv_file_list = File_List( self )

    #--------------------------------------------------------------------
    def create_layout( self ):
        self.main_layout = QtGui.QVBoxLayout( self )
        self.main_layout.addWidget( self.tv_file_list )
        self.setLayout( self.main_layout )

    #--------------------------------------------------------------------
    def get_contents(self):
        self.tv_model.clear()
        self.tv_model.setHorizontalHeaderLabels(["name","date"])
        contents=["path1","path2"]
        for path in contents:
            date = self.get_date(path)
            self.add_file(path,date)
        self.tv_file_list.setColumnWidth(0, 150)

    #--------------------------------------------------------------------
    def add_file(self, name, date):
        name = QtGui.QStandardItem(name)
        name.setToolTip(name.text())
        name.setIcon(self.style().standardIcon(QtGui.QStyle.SP_DirOpenIcon))
        date = QtGui.QStandardItem(date)
        self.tv_model.appendRow([name, date])

    #--------------------------------------------------------------------
    def get_date(self, path):
        return "a date"

    #--------------------------------------------------------------------
    def create_connections( self ):
        self.tv_file_list.clicked.connect( self.on_click )

    # slots --------------------------------------------------------------
    def on_click(self, item ):
        index = self.tv_file_list.selectedIndexes()[0]
        item = self.tv_model.itemFromIndex(index).text()
        print item

############################################
class MyModel(QtGui.QStandardItemModel):
    def __init__(self, parent=None):
        super(MyModel, self).__init__(parent)

    #--------------------------------------------------------------------
    def flags(self, index):
        flag = QtCore.Qt.ItemIsEnabled
        if index.isValid():
            flag |= QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable 
        return flag

############################################
class File_List( QtGui.QTreeView ):
    ''' Create the file filters '''
    def __init__( self, mainUIWindow, parent=get_parent() ):
        super( File_List, self ).__init__( parent )

        self.setModel(mainUIWindow.tv_model)
        self.setIndentation(0)
        self.setColumnWidth(0,500)
        self.setFocusPolicy(QtCore.Qt.NoFocus)
        self.setStyleSheet("QToolTip { color: rgb(170,170,170); background-color: rgb(20,20,20); border: 1px rgb(20,20,20); }")


############################################
if __name__ == "__main__":
    # workaround for a bug in maya
    try:
        tree_view_ui.close()
        tree_view_ui.deleteLater()
    except:
        pass

    tree_view_ui = Main_Window()
    tree_view_ui.show()

    try:
        tree_view_ui.show()
    except:
        tree_view_ui.close()
        tree_view_ui.deleteLater()

解决方案:感谢 ekhumoro 提供快速简便的解决方案!这是更新后的代码和 link 一些帮助我格式化工具提示的 HTML 基础知识:

SOLUTION: Thanks to ekhumoro for the quick and easy solution! Here's the updated code and a link to some HTML basics that helped me format the tooltip:

从 PySide 导入 QtCore、QtGui从 shiboken 导入 wrapInstance将 maya.OpenMayaUI 导入为 mui

from PySide import QtCore, QtGui from shiboken import wrapInstance import maya.OpenMayaUI as mui

def get_parent():
    ptr = mui.MQtUtil.mainWindow()
    return wrapInstance( long( ptr ), QtGui.QWidget )   

############################################        
''' Classes '''
############################################
class Main_Window( QtGui.QDialog ):
    def __init__( self, parent=get_parent() ):
        super( Main_Window, self ).__init__( parent )

        self.create_gui()
        self.create_layout()
        self.create_connections()
        self.get_contents()

    #--------------------------------------------------------------------
    def create_gui( self ):
        self.tv_model=MyModel()
        self.tv_file_list = File_List( self )

    #--------------------------------------------------------------------
    def create_layout( self ):
        self.main_layout = QtGui.QVBoxLayout( self )
        self.main_layout.addWidget( self.tv_file_list )
        self.setLayout( self.main_layout )

    #--------------------------------------------------------------------
    def get_contents(self):
        self.tv_model.clear()
        self.tv_model.setHorizontalHeaderLabels(["name","date"])
        contents=["path1","path2"]
        for path in contents:
            date = self.get_date(path)
            self.add_file(path,date)
        self.tv_file_list.setColumnWidth(0, 150)

    #--------------------------------------------------------------------
    def add_file(self, name, date):
        name = QtGui.QStandardItem(name)
        user_text = "Me"
        image_path = "C:/windows/temp/image_01.png"
        name.setToolTip('<b>{0}</b><br><img src="{1}">'.format(user_text, image_path))
        name.setIcon(self.style().standardIcon(QtGui.QStyle.SP_DirOpenIcon))
        date = QtGui.QStandardItem(date)
        self.tv_model.appendRow([name, date])

    #--------------------------------------------------------------------
    def get_date(self, path):
        return "a date"

    #--------------------------------------------------------------------
    def create_connections( self ):
        self.tv_file_list.clicked.connect( self.on_click )

    # slots --------------------------------------------------------------
    def on_click(self, item ):
        index = self.tv_file_list.selectedIndexes()[0]
        item = self.tv_model.itemFromIndex(index).text()
        print item

############################################
class MyModel(QtGui.QStandardItemModel):
    def __init__(self, parent=None):
        super(MyModel, self).__init__(parent)

    #--------------------------------------------------------------------
    def flags(self, index):
        flag = QtCore.Qt.ItemIsEnabled
        if index.isValid():
            flag |= QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable 
        return flag

############################################
class File_List( QtGui.QTreeView ):
    ''' Create the file filters '''
    def __init__( self, mainUIWindow, parent=get_parent() ):
        super( File_List, self ).__init__( parent )

        self.setModel(mainUIWindow.tv_model)
        self.setIndentation(0)
        self.setColumnWidth(0,500)
        self.setFocusPolicy(QtCore.Qt.NoFocus)
        self.setStyleSheet("QToolTip { color: rgb(170,170,170); background-color: rgb(20,20,20); border: 1px rgb(20,20,20); }")


############################################
if __name__ == "__main__":
    # workaround for a bug in maya
    try:
        tree_view_ui.close()
        tree_view_ui.deleteLater()
    except:
        pass

    tree_view_ui = Main_Window()
    tree_view_ui.show()

    try:
        tree_view_ui.show()
    except:
        tree_view_ui.close()
        tree_view_ui.deleteLater()

推荐答案

工具提示接受富文本,这意味着您可以使用属于 支持的 HTML 子集.这包括一个 img 标签,所以你需要的只是:

Tooltips accept rich-text, which means you can use any markup that is part of the Supported HTML Subset. This includes an img tag, so all you need is something like:

    item.setToolTip('<b>%s</b><br><img src="%s">' % (filename, iconpath))

这篇关于PySide:将图像添加到工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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