PySide6 我可以将回调与其调用方方法同步吗?
PySide6 Can i synchronize a callback with its caller method..?
这是我的代码。
from PySide6 import QtGui,QtCore,QtWidgets,QtWebEngineWidgets
from PySide6.QtWebEngineCore import QWebEngineDownloadRequest, QWebEnginePage
# all these are in some class
self.myView = QtWebEngineWidgets.QWebEngineView()
def getValue(self):
self.myView.page().runJavaScript(
'''
(function () {
finds = document.getElementsByClassName('ps');
if (finds.length > 0) return finds[0].offsetWidth;
return -1;
})();
''', 0, self._getvalueCallback)
# i want to wait for the callback and return the value.
# want to make it to work synchronously.
def _getvalueCallback(self, value):
if type(value) is float:
self._value = int(value)
else:
self._value = -1
这可能吗..?我尝试使用 threading.Event
但无法正常工作,因为 event.waiting()
只会阻塞线程和回调。
您可以使用 QEventLoop:
from collections import deque
from functools import partial
from PySide6 import QtGui, QtCore, QtWidgets, QtWebEngineWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.myView = QtWebEngineWidgets.QWebEngineView()
self.setCentralWidget(self.myView)
self.myView.load(QtCore.QUrl("https://whosebug.com/"))
QtCore.QTimer.singleShot(4000, self.handle_timeout)
def handle_timeout(self):
value = self.getValue()
print("value:", value)
def getValue(self):
q = deque()
loop = QtCore.QEventLoop()
self.myView.page().runJavaScript(
"""
(function () {
finds = document.getElementsByClassName('ps');
if (finds.length > 0) return finds[0].offsetWidth;
return -1;
})();
""",
0,
partial(self._getvalueCallback, loop, q),
)
loop.exec()
return q.pop()
def _getvalueCallback(self, loop, q, value):
q.append(int(value) if isinstance(value, float) else -1)
loop.quit()
if __name__ == "__main__":
app = QtWidgets.QApplication()
mw = MainWindow()
mw.show()
app.exec()
这是我的代码。
from PySide6 import QtGui,QtCore,QtWidgets,QtWebEngineWidgets
from PySide6.QtWebEngineCore import QWebEngineDownloadRequest, QWebEnginePage
# all these are in some class
self.myView = QtWebEngineWidgets.QWebEngineView()
def getValue(self):
self.myView.page().runJavaScript(
'''
(function () {
finds = document.getElementsByClassName('ps');
if (finds.length > 0) return finds[0].offsetWidth;
return -1;
})();
''', 0, self._getvalueCallback)
# i want to wait for the callback and return the value.
# want to make it to work synchronously.
def _getvalueCallback(self, value):
if type(value) is float:
self._value = int(value)
else:
self._value = -1
这可能吗..?我尝试使用 threading.Event
但无法正常工作,因为 event.waiting()
只会阻塞线程和回调。
您可以使用 QEventLoop:
from collections import deque
from functools import partial
from PySide6 import QtGui, QtCore, QtWidgets, QtWebEngineWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.myView = QtWebEngineWidgets.QWebEngineView()
self.setCentralWidget(self.myView)
self.myView.load(QtCore.QUrl("https://whosebug.com/"))
QtCore.QTimer.singleShot(4000, self.handle_timeout)
def handle_timeout(self):
value = self.getValue()
print("value:", value)
def getValue(self):
q = deque()
loop = QtCore.QEventLoop()
self.myView.page().runJavaScript(
"""
(function () {
finds = document.getElementsByClassName('ps');
if (finds.length > 0) return finds[0].offsetWidth;
return -1;
})();
""",
0,
partial(self._getvalueCallback, loop, q),
)
loop.exec()
return q.pop()
def _getvalueCallback(self, loop, q, value):
q.append(int(value) if isinstance(value, float) else -1)
loop.quit()
if __name__ == "__main__":
app = QtWidgets.QApplication()
mw = MainWindow()
mw.show()
app.exec()