Advertisement
Guest User

Untitled

a guest
Nov 14th, 2013
98
0
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.container = container
  10.         self.toolData = data
  11.         self.title = data['title']
  12.         self.author = data['author']
  13.        
  14.     def data(self, role=QtCore.Qt.UserRole + 1):
  15.         '''
  16.        Return the name for displaying only,
  17.        otherwise return the entire object.
  18.        '''
  19.         if role == Qt.DisplayRole:
  20.             return self.title
  21.         return QtGui.QStandardItem.data(self, role)
  22.  
  23.  
  24. class TestWidget(QtGui.QWidget):
  25.    
  26.     def __init__(self, parent=None):
  27.         super(TestWidget, self).__init__(parent)
  28.         self.menu = QtGui.QMenu()
  29.         self.btn = QtGui.QPushButton('sender test')
  30.         self.btn.setMenu(self.menu)
  31.         self.setLayout(QtGui.QHBoxLayout())
  32.         self.layout().addWidget(self.btn)
  33.        
  34.     def testSlot(self):
  35.         sender = self.sender()
  36.         print sender.data()
  37.  
  38.  
  39. if __name__ == '__main__':
  40.     import sys
  41.    
  42.     app = QtGui.QApplication([])
  43.     w = TestWidget()
  44.     actionDef = QtGui.QAction('dict object', w)
  45.     actionCustom = QtGui.QAction('custom object', w)
  46.     rawData = {'title':'toolA', 'author':'some dude'}
  47.     testItem = TestItem('test container', rawData)
  48.    
  49.     # adding dict object is passed on by sender()
  50.     actionDef.setData(rawData)
  51.     # custom object is not passed on by sender() and sender().data() returns None
  52.     actionCustom.setData(testItem)
  53.    
  54.     actionDef.triggered.connect(w.testSlot)
  55.     actionCustom.triggered.connect(w.testSlot)
  56.    
  57.     w.menu.addAction(actionDef)
  58.     w.menu.addAction(actionCustom)
  59.     w.show()
  60.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement