QMetaObject::Connection equivalent

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

QMetaObject::Connection equivalent

Florian Bruhin
Hi,

I just noticed Qt5 returns a QMetaObject::Connection object when using
QObject::connect:

http://doc.qt.io/qt-5/qobject.html#connect-2
http://doc.qt.io/qt-5/qmetaobject-connection.html

You can then use that with the appropriate QObject::disconnect method
to disconnect e.g. lambdas easily again:

http://doc.qt.io/qt-5/qobject.html#disconnect-4

I'm missing something like that in Python - it'd be nice to be able to
do something like this:

    w = QTextEdit()
    connection = w.textChanged.connect(lambda text: None)
    # [...]
    w.textChanged.disconnect(connection)

'connection' then would be some pyqtSignalConnection object which
pyqtSignal.disconnect understands.

Alternatively (or in addition to the above), disconnect could be a
method of pyqtSignalConnection:

    w = QTextEdit()
    connection = w.textChanged.connect(lambda text: None)
    # [...]
    connection.disconnect()

This would make it easier to work with Python lambda's and
pyqtSignals.

What do you think?

Florian

--
http://www.the-compiler.org | [hidden email] (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    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QMetaObject::Connection equivalent

Kyle Altendorf

On 2016-03-02 10:09, Florian Bruhin wrote:

> Hi,
>
> I just noticed Qt5 returns a QMetaObject::Connection object when using
> QObject::connect:
>
> http://doc.qt.io/qt-5/qobject.html#connect-2
> http://doc.qt.io/qt-5/qmetaobject-connection.html
>
> You can then use that with the appropriate QObject::disconnect method
> to disconnect e.g. lambdas easily again:
>
> http://doc.qt.io/qt-5/qobject.html#disconnect-4
>
> I'm missing something like that in Python - it'd be nice to be able to
> do something like this:
>
>     w = QTextEdit()
>     connection = w.textChanged.connect(lambda text: None)
>     # [...]
>     w.textChanged.disconnect(connection)
>
> 'connection' then would be some pyqtSignalConnection object which
> pyqtSignal.disconnect understands.
>
> Alternatively (or in addition to the above), disconnect could be a
> method of pyqtSignalConnection:
>
>     w = QTextEdit()
>     connection = w.textChanged.connect(lambda text: None)
>     # [...]
>     connection.disconnect()
>
> This would make it easier to work with Python lambda's and
> pyqtSignals.
>
> What do you think?

Seems like it would be nice.  Is there no other interest in this (old)
topic?  I made a little workaround class.

https://gist.github.com/altendky/1f95db4924732341d945b04157c6e94a
```
class Connection:
     def __init__(self, signal, slot):
         signal.connect(slot)
         self.signal = signal
         self.slot = slot

     def disconnect(self):
         self.signal.disconnect(self.slot)
```

Used like `Connection(obj_a.signal, obj_b.slot)`.  Seems like something
along these lines could be returned by `pyqtSignal.connect()`.  If not a
'real' `QMetaObject::Connection`.  Perhaps it would be appropriate to
use weakrefs, I'm not certain.

Cheers,
-kyle
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: QMetaObject::Connection equivalent

Phil Thompson-5
In reply to this post by Florian Bruhin
On 2 Mar 2016, at 3:09 pm, Florian Bruhin <[hidden email]> wrote:

>
> Hi,
>
> I just noticed Qt5 returns a QMetaObject::Connection object when using
> QObject::connect:
>
> http://doc.qt.io/qt-5/qobject.html#connect-2
> http://doc.qt.io/qt-5/qmetaobject-connection.html
>
> You can then use that with the appropriate QObject::disconnect method
> to disconnect e.g. lambdas easily again:
>
> http://doc.qt.io/qt-5/qobject.html#disconnect-4
>
> I'm missing something like that in Python - it'd be nice to be able to
> do something like this:
>
>    w = QTextEdit()
>    connection = w.textChanged.connect(lambda text: None)
>    # [...]
>    w.textChanged.disconnect(connection)
>
> 'connection' then would be some pyqtSignalConnection object which
> pyqtSignal.disconnect understands.
>
> Alternatively (or in addition to the above), disconnect could be a
> method of pyqtSignalConnection:
>
>    w = QTextEdit()
>    connection = w.textChanged.connect(lambda text: None)
>    # [...]
>    connection.disconnect()
>
> This would make it easier to work with Python lambda's and
> pyqtSignals.
>
> What do you think?

I don't know I've had a blind spot about this...

QMetaObject.Connection is now wrapped, is returned by connect() and can be passed to disconnect().

...in tonight's snapshot.

Phil
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt