PDA

View Full Version : PyQt : Signal like this



prashant
29th September 2009, 20:09
I am creating <QAction> in a loop and adding it to menu.



for fileName in fileNames:
action = QtGui.QAction(QtGui.QIcon(iconFile), fileName, menu)
menu.addAction(action)


I need to connect this action to a python function in such a way that I should get <text> of QAction <fileName> or instance of QAction object itself.

For example If action name is required


def contextMenuAction(self, actionName):
print actionName

If action object is required


def contextMenuAction(self, actionObject):
myData = actionObject.data()


How do I setup action and slot? Logically it would be like this:


self.connect(action, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("contextMenuAction(str)"), fileName)
self.connect(action, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("contextMenuAction(object)"), action)

wysota
30th September 2009, 13:12
Take a look at QSignalMapper.

prashant
2nd October 2009, 19:18
QxColorPicker has been created by sub classing QtGui.QWidget.

The old style connection method is working fine.


// Connect picker->slider
#QtCore.QObject().connect(self.qColorPicker, QtCore.SIGNAL("brightnessChanged(int)"),
#self.qSlider, QtCore.SLOT("setValue(int)"))


The new style method is not working.


self.qColorPicker.brightnessChanged.connect(self.q Slider.setValue)


I am getting an error saying:


AttributeError: 'QxColorPicker' object has no attribute 'brightnessChanged'


In <QxColorPicker>, I am emitting <brightnessChanged> signal at two places like this:


self.emit(QtCore.SIGNAL("brightnessChanged(int)", self.myColor.value()))


suggestions?

wysota
2nd October 2009, 19:56
But what do you want from us? :)

prashant
2nd October 2009, 20:43
What's wrong with my new style syntax? Why it's not working?


self.qColorPicker.brightnessChanged.connect(self.q Slider.setValue)

wysota
2nd October 2009, 21:03
Obviously the object has no such attribute.