cannot get clipboard text using QClipboard on Android when App in background
Solved
Mobile and Embedded
-
I am using the following code to retrieve clipboard text once clipboard data changed on Android
from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class EngkuDict(QLabel): # Permissions QFile::permissions() const def __init__(self, parent=None): super().__init__(parent) self.showMaximized() self.clipboard = QGuiApplication.clipboard() self.clipboard.dataChanged.connect(self.clipboardTextChanged) # self.clipboard.changed.connect(self.clipboardModeChanged) @pyqtSlot() def clipboardTextChanged(self): selectedText = self.clipboard.text() self.setText('clipboardTextChanged: {}'.format(selectedText)) def clipboardModeChanged(self, mode): selectedText = self.clipboard.text() self.setText('mode: {}\n{}'.format(mode, selectedText)) if __name__ == "__main__": import sys app = QApplication(sys.argv) # QApplication.setQuitOnLastWindowClosed(False) trayIcon = EngkuDict() trayIcon.show() sys.exit(app.exec_())
when the code is running in background, I copied text in another application, this application just cannot get clipboard text, so how to solve the issue ?
-
Add a slot handling for QGuiAppllication::applicationStateChanged. When it changes to
Qt::ApplicationActive
, call QClipboard::text() to update the value. -
Thanks ! But I want it to work constantly in the background even when not activated .
-
@redstoneleo Apps in background are stopped, aren't they? You would need a background service.