[PyQt] converting old signal/slot to new signal/slot

C C coastalchaos at hotmail.co.uk
Thu May 28 14:53:08 BST 2015


Florian,

Thanks.

Yes, I have put pyqtSignal() in my code.

Here's the old style

class DatabaseProxy(Proxy, QObject):
    databaseChanged = pyqtSignal(object)
  
    def __init__(self, url):
        # Call superclasses
        Proxy.__init__(
            self, self.NAME, DatabaseAdapter(url))
        QtCore.QObject.__init__(self)

        # Public attributes
        self.session = None    
    def onRegister(self):
        """
        Create a new database session
        """
        # Create session
        self.session = self.new_event_session()
        self.connect(
            QtCore.QCoreApplication.instance(),
            QtCore.SIGNAL(b"databaseChanged(PyQt_PyObject)"),
            self.on_database_updated_signal,
            Qt.QueuedConnection
            )



Here's the new style:-


class DatabaseProxy(Proxy, QObject):
    databaseChanged = pyqtSignal(object)     

    def __init__(self, url):
        # Call superclasses
        Proxy.__init__(
            self, self.NAME, DatabaseAdapter(url))
        QtCore.QObject.__init__(self)

        # Public attributes
        self.session = None        
    
    def onRegister(self):
        """
        Create a new database session
        """
        # Create session
        self.session = self.new_event_session()    
        self.databaseChanged.connect(
            self.on_database_updated_signal,
                        Qt.QueuedConnection)




The only thing I'm trying to work out is what happened to QtCore.QCoreApplication.instance() in the new style?
Do i need to bring it back into play somehow? I'm just trying to understand the old style in this instance so I can ensure that I port from 4 to 5 cleanly without breaking functionality.


Now i've changed the signal to the new style it not longer errors in the eclipse runner/debugger. 


Rob




On 28 May 2015, at 13:15, Florian Bruhin <me at the-compiler.org> wrote:

>> It looks as if you are trying to have your signal not just exist but pass a
>> parameter? The syntax for that in pyQtSignal is a bit obscure and I'm not
>> comfortable with it so maybe somebody else can write about that.
> 
> It's quite simple, IMHO (at least as you don't need overloaded
> signals):
> 
>    class myWidget( QWidget ):
>        databaseChanged = pyqtSignal(str)
> 
>    [...]
> 
>    # self.react_to_db is a callable taking one argument.
>    self.databaseChanged.connect( self.react_to_db )
> 
>    [...]
> 
>    if database_has_changed :
>        # database_name has to be a string
>        self.databaseChanged.emit(database_name)
> 
> (also, note that it's pyqtSignal, not pyQtSignal)
> 
> Florian
> 
> -- 
> http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
>   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
>         I love long mails! | http://email.is-not-s.ms/
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150528/73fb5648/attachment.html>


More information about the PyQt mailing list