0

QClipboard::dataChanged() was not emitted in QtWebEngine context with pyqt6 or pyside6, but it works fine with qt5/pyqt5, how to solve it ? Environment:

Python: 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
Flavor: Unknown
Executable: C:\Users\Oscar\AppData\Local\Programs\Python\Python39\python.exe
OS: Windows
Arch: x86_64
WindowsRelease: 10

>>> from PyQt6.QtCore import *
>>> QT_VERSION_STR
'6.2.3'
>>> PYQT_VERSION_STR
'6.2.3'
>>> 

from PyQt6.QtCore import *
from PyQt6.QtCore import pyqtSlot as Slot
from PyQt6.QtCore import pyqtSignal as Signal
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import *
from PyQt6.QtWebEngineCore import *
import sys


class WebEngineView(QWebEngineView):  #

    def __init__(self, parent=None):
        super().__init__(parent)
        self.page().settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
        self.load(QUrl('https://doc.qt.io/qt-6/qwebenginesettings.html'))
        self.clipboard = QGuiApplication.clipboard()
        self.clipboard.dataChanged.connect(self.clipboardTextChanged)

    @Slot()
    def clipboardTextChanged(self):
        self.copiedText = self.clipboard.text()

        print(self.sender(), self.copiedText)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec())
iMath
  • 2,326
  • 2
  • 43
  • 75
  • Works fine for me using qt-6.2.3 and pyqt-6.2.3 on linux. – ekhumoro Mar 19 '22 at 11:35
  • @ekhumoro just added my test environment info – iMath Mar 19 '22 at 11:39
  • Where is the info about qt/pyqt? Are you using the latest versions? Have you tried testing with qt5/pyqt5? – ekhumoro Mar 19 '22 at 11:50
  • @ekhumoro Thank you! Tested with version 6.2.3. Tested with qt5/pyqt5 works fine but not pyqt6 or pyside6. – iMath Mar 19 '22 at 11:53
  • 1
    Well, then it seems to be a qt6 bug that only(?) affects windows. There's no obvious existing issue on the [bug tracker](https://bugreports.qt.io/secure/Dashboard.jspa) - but I didn't do a thorough search. – ekhumoro Mar 19 '22 at 12:16

1 Answers1

0

I find the solution here

The sandbox somehow blocks the WM_CLIPBOARDUPDATE message ( https://learn.microsoft.com/en-us/windows/win32/dataxchg/wm-clipboardupdate ) and QPA can't notify about clipboard changes.

The sandbox on windows is enabled since https://codereview.qt-project.org/c/qt/qtwebengine/+/276060

neosettler, as a workaround you can disable the sandbox by the --no-sandbox command line argument or by setting the QTWEBENGINE_DISABLE_SANDBOX environment variable.

Then I changed my code to:

from PyQt6.QtCore import *
from PyQt6.QtCore import pyqtSlot as Slot
from PyQt6.QtCore import pyqtSignal as Signal
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import *
from PyQt6.QtWebEngineCore import *
import sys
import os


class WebEngineView(QWebEngineView):  #

    def __init__(self, parent=None):
        super().__init__(parent)
        self.page().settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
        self.load(QUrl('https://doc.qt.io/qt-6/qwebenginesettings.html'))
        self.clipboard = QGuiApplication.clipboard()
        self.clipboard.dataChanged.connect(self.clipboardTextChanged)

    @Slot()
    def clipboardTextChanged(self):
        self.copiedText = self.clipboard.text()

        print(self.copiedText)


if __name__ == "__main__":
    sys.argv.append('--no-sandbox')
    print(sys.argv)
    app = QApplication(sys.argv)

    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec())
iMath
  • 2,326
  • 2
  • 43
  • 75