Guest User

Untitled

a guest
Nov 13th, 2013
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PySide import QtCore
  2. from PySide import QtGui
  3.  
  4. class TestItem(QtGui.QStandardItem):
  5.     '''Item that stores data for a tool'''
  6.  
  7.     def __init__(self, container, data):
  8.         super(TestItem, self).__init__(data['title'])
  9.         self.data = data
  10.         self.container = container
  11.         self.toolData = data
  12.         self.title = data['title']
  13.         self.author = data['author']
  14.        
  15.     def data(self, role=QtCore.Qt.UserRole + 1):
  16.         '''
  17.        Return the name for displaying only,
  18.        otherwise return the entire object.
  19.        '''
  20.         if role == Qt.DisplayRole:
  21.             return self.title
  22.         return QtGui.QStandardItem.data(self, role)
  23.  
  24.  
  25. class TestWidget(QtGui.QWidget):
  26.    
  27.     def __init__(self, parent=None):
  28.         super(TestWidget, self).__init__(parent)
  29.         self.menu = QtGui.QMenu()
  30.         self.btn = QtGui.QPushButton('sender test')
  31.         self.btn.setMenu(self.menu)
  32.         self.setLayout(QtGui.QHBoxLayout())
  33.         self.layout().addWidget(self.btn)
  34.        
  35.     def testSlot(self):
  36.         sender = self.sender()
  37.         print sender.data()
  38.  
  39.  
  40. if __name__ == '__main__':
  41.     import sys
  42.    
  43.     app = QtGui.QApplication([])
  44.     w = TestWidget()
  45.     actionDef = QtGui.QAction('dict object', w)
  46.     actionCustom = QtGui.QAction('custom object', w)
  47.     rawData = {'title':'toolA', 'author':'some dude'}
  48.     testItem = TestItem('test container', rawData)
  49.    
  50.     # adding dict object is passed on by sender()
  51.     actionDef.setData(rawData)
  52.     # custom object is not passed on by sender() and sender().data() returns None
  53.     actionCustom.setData(testItem)
  54.    
  55.     actionDef.triggered.connect(w.testSlot)
  56.     actionCustom.triggered.connect(w.testSlot)
  57.    
  58.     w.menu.addAction(actionDef)
  59.     w.menu.addAction(actionCustom)
  60.     w.show()
  61.     sys.exit(app.exec_())
RAW Paste Data