![]() |
2010-08-19
, 20:33
|
|
Posts: 963 |
Thanked: 626 times |
Joined on Sep 2009
@ Connecticut, USA
|
#2
|
![]() |
2010-08-20
, 10:19
|
Posts: 15 |
Thanked: 87 times |
Joined on Dec 2009
|
#3
|
In PyQt I need to define a lambda function to accomplish the same, in PySide I need to use some @ directives to create a specific signal with parameters or something. On top of other aggravation PyQt and PySide need to have different syntaxes/constraints for doing this.
QtCore.pyqtSignal = QtCore.Signal QtCore.pyqtSlot = QtCore.Slot
#!/usr/bin/python import sys from PySide import QtCore # define a new slot that receives a string and has # 'saySomeWords' as its name @QtCore.Slot(str) def saySomeWords(words): print words class Communicate(QtCore.QObject): # create a new signal on the fly and name it 'speak' speak = QtCore.Signal(str) someone = Communicate() # connect signal and slot someone.speak.connect(saySomeWords) # emit 'speak' signal someone.speak.emit("Hello everybody!")
![]() |
2010-08-22
, 18:40
|
Posts: 79 |
Thanked: 20 times |
Joined on Apr 2010
|
#4
|
def link(source, signal, target, *args, **kwargs) : proxy_target = functools.partial(target, *args, **kwargs) source.connect(source, SIGNAL(signal), proxy_target)
link(web_page, "loadFinished(bool)", web_page.load_finished)
def linkSource(source, signal, target, *args, **kwargs) : link(source, signal, target, *((source,) + args), **kwargs) def linkClick(source, target, *args, **kwargs) : link(source, "clicked()", target, *args, **kwargs)
linkClick(emailbutton, stack.setCurrentWidget, emailarea) linkClick(calendarbutton, stack.setCurrentWidget, calendararea) linkClick(contactbutton, stack.setCurrentWidget, contactarea)
link(self, "kick_next_load()", self.load_next)
self.emit(SIGNAL("kick_next_load()"))
![]() |
2010-08-23
, 07:45
|
Posts: 15 |
Thanked: 87 times |
Joined on Dec 2009
|
#5
|
Thanks guys. I seemed to encounter another problem with PySide that I did not have with PyQt: it is not allowed to link a signal to a Python method that is part of a class that inherits one of the Qt classes, apparently because the inherited class is still C++, if you ask PySide, and you are not allowed to define dynamic slots in PySide Thus, following rm42's lead I defined the following convenience function:
Now. all this works for me, but is it somehow wrong or against how Qt and PySide are intended to work? Especially, I do not see a lot of use for the new-style signals and slots, so I know there must be something I am not getting here.
![]() |
2010-08-23
, 19:38
|
Posts: 79 |
Thanked: 20 times |
Joined on Apr 2010
|
#7
|
![]() |
2010-08-24
, 09:55
|
Posts: 15 |
Thanked: 87 times |
Joined on Dec 2009
|
#8
|
Ok, as the bug now states, the problem only seems to affect classes inherited from QApplication.
Couple of further notes while we are on the subject:
1) Would appreciate it if the pyside documentation covered the basic use cases first: how to connect a signal to some function (this is covered) and how to connect a signal with a parameter to a function - e.g. how to connect the loadFinished(bool) signal of QWebPage using the new syntax? Should I use the bracket notation of the last example? What about signals with multiple parameters? Are there any in Qt with multiple parameters, for that matter?
import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * def finished(succeeded): print succeeded app = QApplication(sys.argv) web = QWebView() web.loadFinished.connect(finished) web.load(QUrl("http://www.google.com")) web.show() sys.exit(app.exec_())
2) Would it be possible to extend the new syntax to allow including parameters after the function name, i.e. widget.signal.connect(function, arg1, arg2, ...)?
import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * def loadFinished(s,succeeded): print s,succeeded app = QApplication(sys.argv) web = QWebView() web.loadFinished.connect(lambda b: loadFinished("I'm custom!",b)) web.load(QUrl("http://www.google.com")) web.show() sys.exit(app.exec_())
I specifically have problems with a case where I want to provide some arguments to a function called as a result of a signal.
Take for example the case of clicking a button and this calling a function with some specific argument, say a string to be printed.
In PyGtk I can say:
button.connect("clicked", print_function, "String to print")
And define the function with nothing extra special to it:
def print_function(widget, print_string) : print print_string
In PyQt I need to define a lambda function to accomplish the same, in PySide I need to use some @ directives to create a specific signal with parameters or something. On top of other aggravation PyQt and PySide need to have different syntaxes/constraints for doing this.
A more realistic example is creating a QStackedArea and some buttons to change what is shown in the area - having one function do the change, with the different buttons passing different content to show, seems inordinately complicated to accomplish, at least when comparing to the simplicity of PyGtk.
Since I am new to this Qt Python stuff there must be something I am missing. What is the correct/easy/elegant way of passing some not API-defined extra parameters to the target of a signal?