QApplication.clipboard() segfault on GNU/Linux

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

QApplication.clipboard() segfault on GNU/Linux

Tiger-222
Hello,

I am using Python 3.7.3 with the latest PyQt 5.13.0 on Debian GNU/Linux 9.
In my app, I got a segfault, and I think this is related to what happens
below:

    >>> from PyQt5.QtWidgets import QApplication
    >>> QApplication([]).clipboard().setText("azerty")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    RuntimeError: wrapped C/C++ object of type QClipboard has been deleted
    >>> cb = QApplication([]).clipboard()
    >>> cb
    <PyQt5.QtGui.QClipboard object at 0x7f514d298708>
    >>> cb.setText("azerty")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    RuntimeError: wrapped C/C++ object of type QClipboard has been deleted

Is it desired? Or am I doing something wrong?

Thanks!



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr 
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
Reply | Threaded
Open this post in threaded view
|

Re: QApplication.clipboard() segfault on GNU/Linux

Tiger-222
FTR I have the same error on macOS.



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr 
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
Reply | Threaded
Open this post in threaded view
|

Re: QApplication.clipboard() segfault on GNU/Linux

Hugo Slepicka

This is what worked for me for all OSs:


clipboard = QApplication.clipboard()

clipboard.setText(copy_text)

event = QEvent(QEvent.Clipboard)

QApplication.instance().sendEvent(clipboard, event)



On Tue, Jul 23, 2019 at 9:08 AM Tiger-222 <[hidden email]> wrote:
FTR I have the same error on macOS.



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
--
Hugo Slepicka
+1 631 855 5717

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

Re: QApplication.clipboard() segfault on GNU/Linux

BPL
To really understand this I'd suggest you also reading these 2 related posts:


and


That said, with the current latest pyqt you can guarantee the data copied into the clipboard will be preserved 
by using a QApplication context manager, like this:

from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':
    FORCE_BUG = True

    if not FORCE_BUG:
        with QApplication([]) as app:
            clipboard = app.clipboard()
            clipboard.setText("azerty")
            print(QApplication.instance().clipboard().text())
            print(app.clipboard().text())
            print(clipboard.text())
    else:
        app = QApplication([])
        clipboard = app.clipboard()
        clipboard.setText("azerty")
        print(QApplication.instance().clipboard().text())
        print(app.clipboard().text())
        print(clipboard.text())

Ideally it wouldn't be necessary to use any context manager at all... but not sure how difficult would be for Phil
to provide that behaviour :/

 

On Tue, Jul 23, 2019 at 6:29 PM Hugo Slepicka <[hidden email]> wrote:

This is what worked for me for all OSs:


clipboard = QApplication.clipboard()

clipboard.setText(copy_text)

event = QEvent(QEvent.Clipboard)

QApplication.instance().sendEvent(clipboard, event)



On Tue, Jul 23, 2019 at 9:08 AM Tiger-222 <[hidden email]> wrote:
FTR I have the same error on macOS.



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
--
Hugo Slepicka
+1 631 855 5717
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

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

Re: QApplication.clipboard() segfault on GNU/Linux

Tiger-222
In reply to this post by Tiger-222
Oh I see what is missing from my original message :o
This is the snippet that fails on a segfault:

    >>> from PyQt5.QtCore import QCoreApplication
    >>> from PyQt5.QtWidgets import QApplication
    >>> with QCoreApplication([]) as app:
    ...    print(QApplication.clipboard().text())

And using QApplication.instance().clipboard().text() will return
"AttributeError: 'QCoreApplication' object has no attribute 'clipboard'".

The clipboard is only something used in the GUI world? And so it is not
accessible via QCoreApplication?



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr 
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
BPL
Reply | Threaded
Open this post in threaded view
|

Re: QApplication.clipboard() segfault on GNU/Linux

BPL
That's right, inheritance goes like this:

QCoreApplication -> QGuiApplication -> QApplication 

And clipboard is a public static member of QGuiApplication

On Wed, Jul 24, 2019 at 10:13 AM Tiger-222 <[hidden email]> wrote:
Oh I see what is missing from my original message :o
This is the snippet that fails on a segfault:

    >>> from PyQt5.QtCore import QCoreApplication
    >>> from PyQt5.QtWidgets import QApplication
    >>> with QCoreApplication([]) as app:
    ...    print(QApplication.clipboard().text())

And using QApplication.instance().clipboard().text() will return
"AttributeError: 'QCoreApplication' object has no attribute 'clipboard'".

The clipboard is only something used in the GUI world? And so it is not
accessible via QCoreApplication?



-----
Mickaël Schoentgen.
[FR] Personal blog: http://www.tiger-222.fr
--
Sent from: http://python.6.x6.nabble.com/PyQt-f1792048.html
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

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