Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PySide import QtCore
- from PySide import QtGui
- class TestItem(QtGui.QStandardItem):
- '''Item that stores data for a tool'''
- def __init__(self, container, data):
- super(TestItem, self).__init__(data['title'])
- self.container = container
- self.toolData = data
- self.title = data['title']
- self.author = data['author']
- def data(self, role=QtCore.Qt.UserRole + 1):
- '''
- Return the name for displaying only,
- otherwise return the entire object.
- '''
- if role == Qt.DisplayRole:
- return self.title
- return QtGui.QStandardItem.data(self, role)
- class TestWidget(QtGui.QWidget):
- def __init__(self, parent=None):
- super(TestWidget, self).__init__(parent)
- self.menu = QtGui.QMenu()
- self.btn = QtGui.QPushButton('sender test')
- self.btn.setMenu(self.menu)
- self.setLayout(QtGui.QHBoxLayout())
- self.layout().addWidget(self.btn)
- def testSlot(self):
- sender = self.sender()
- print sender.data()
- if __name__ == '__main__':
- import sys
- app = QtGui.QApplication([])
- w = TestWidget()
- actionDef = QtGui.QAction('dict object', w)
- actionCustom = QtGui.QAction('custom object', w)
- rawData = {'title':'toolA', 'author':'some dude'}
- testItem = TestItem('test container', rawData)
- # adding dict object is passed on by sender()
- actionDef.setData(rawData)
- # custom object is not passed on by sender() and sender().data() returns None
- actionCustom.setData(testItem)
- actionDef.triggered.connect(w.testSlot)
- actionCustom.triggered.connect(w.testSlot)
- w.menu.addAction(actionDef)
- w.menu.addAction(actionCustom)
- w.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement