2015-07-09 85 views
1

我正在構建一個列出目錄中的文件的工具。當您將鼠標懸停在每個文件上時,它會顯示一個顯示日期的工具提示以及製作文件的人員。我還想要一張圖片來陪伴這些數據。PySide:將圖像添加到工具提示

是否可以將圖像插入工具提示?對於QTreeView中的每個項目,我想要一個特定的圖像和文本彈出。如果使用工具提示無法完成,那麼還有其他選擇嗎?

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() 

SOLUTION: 感謝ekhumoro的快速和簡單的解決方案! 下面是更新代碼和link一些HTML基礎知識,幫助我格式化提示:

從PySide進口QtCore,QtGui 從shiboken進口wrapInstance 進口maya.OpenMayaUI爲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() 

回答

2

工具提示接受富文本,這意味着您可以使用Supported HTML Subset中的任何標記。這包括一個img標籤,因此您需要的所有內容都是這樣的:

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

工作就像一個魅力,謝謝! –