Unsolved Q_ARG missing in python/ how to use invokeMethod ?
-
Hi,
I'm trying to use QMetaObject.invokeMethod wich needs some PySide2.QtCore.QGenericArgument wich has to be provided by the Q_ARG macro (https://doc.qt.io/qtforpython-5.12/PySide2/QtCore/QGenericArgument.html#PySide2.QtCore.QGenericArgument)Actually I can't find this Q_ARG macro in QtCore or anywhere else. where is it ?
More generally how to use invokeMethod without it ?
Jimmy -
I already saw it.
it import Q_ARG from QtCore but it doesn't exist in PySide2. I can't import it -
@Jim-Gir
Have you read e.g. https://stackoverflow.com/questions/23538904/qmetaobjectinvokemethod-doesnt-find-methods-with-parameters and other hits discussing this from Googlingpyside2 q_arg
? -
Hi,
Well, it looks like there's indeed something off here with Q_ARG, did you already check the bug report system ? If you don't find anything there please open a report providing a minimal runnable sample that reproduces the problem.
-
@Jim-Gir
I also meant to draw your attention to https://stackoverflow.com/questions/53296261/usage-of-qgenericargument-q-arg-when-using-invokemethod-in-pyside2. Which says there still isn't anyQ_ARG
in PySide2 and you can't do what you with no workaround :(As they point out in the answer that is a bug, it does not matter if it is QString or str, which still remains in PySide2 and unfortunately there is no workaround – eyllanesc Nov 14 '18 at 15:35
-
someone reported the bug?
the problem is from pyside qt4
For the moment I resolved like this:in qml, file.qml: ApplicationWindow{ id:root property var argsTemp Item{ id: myitem objectName:"myitem" function myFunctionBrige (){ myFunction(argsTemp[0]); argsTemp=[]; } function myFunction(arg){ console.log("myargLost",arg); } } in python: engine.load(QUrl.fromLocalFile('file.qml')) root = engine.rootObjects()[0] root.setProperty("argsTemp",["arg1"]) object_qml = root.findChild(QObject, "myitem") QMetaObject.invokeMethod(object_qml, "myFunctionBrige")
-
Use pyqt5 instead of pyside2
or Use signal slots instead of invokeMethod
@Jim-Gir -
but when are many functions? ...
and if you need to invoke from native code to qml?using qt5 with c ++ no problem,
but PySide2, the Q_ARG macro is missing -
@numaelisis
Is this a question? Yes, PySide2 does not have any support forQ_ARG
. -
-
For myself, I came across this when trying to use an example written in PyQt5 and invokeMethod in PySide2.
The example was using invokeMethod to return a QImage from a separate thread that had done some processing.An alternative solution that (with minimal testing) works is to create a signal object, and connect it to a slot via a queued connection.
from PySide2 import QtCore, QtGui class MyClass(QtCore.QObject): imageReady = QtCore.Signal(QtGui.QImage) def __init__(self): super() self.imageReady.connect(self.setImage, QtCore.Qt.QueuedConnection) def processNewImageOnThread(image): threading.Thread(target=self.process_image, args=(image)).start() def process_image(image): # Process image here self.imageReady.emit(image) @QtCore.Slot(QtGui.QImage) def setZoomedImage(self, image): # set this image on the viewmodel, which needs to be done on the GUI thread. VM.zoomedImage = image
I believe this example can be expanded to any set of arguments.