[Interest] Python staticmethod slot
Jérôme Godbout
godboutj at amotus.ca
Wed Feb 5 16:29:59 CET 2020
__________________
Python code:
__________________
import math
from PySide2.QtCore import QObject
from PySide2.QtGui import QVector3D
class PointHelper(QObject):
@staticmethod
@Slot(QVector3D, result=bool)
def pose_position_is_nan(pose: QVector3D) -> bool:
"""This does not work"""
return math.isnan(pose.x()) or math.isnan(pose.y())
@Slot(QVector3D, result=bool)
def pose_position_is_nan2(self, pose: QVector3D) -> bool:
"""This does work"""
return math.isnan(pose.x()) or math.isnan(pose.y())
...
qmlRegisterType(PointHelper, "Toto", 1, 0, 'PointHelper')
______________
Qml
______________
import Toto 1.0
PointHelper
{
id: pointHelper_
Component.onCompleted:
{
pointHelper_.pose_position_is_nan2(Qt.vector3d(0,0,0)) // this does work
pointHelper_.pose_position_is_nan(Qt.vector3d(0,0,0)) // this does not work, console log: MyFile.qml:63: TypeError: Property 'pose_position_is_nan' of object PointHelper(0x1988c36ba10) is not a function
PointHelper.pose_position_is_nan(Qt.vector3d(0,0,0)) // this does not work, console log: MyFile.qml:64: TypeError: Property 'pose_position_is_nan' of object [object Object] is not a function
}
}
Maybe I'm missing something?! If you want actual minimal running example I can provide it.
Thanks,
Jerome
-----Original Message-----
From: Interest <interest-bounces at qt-project.org> On Behalf Of Cristián Maureira-Fredes
Sent: February 5, 2020 9:42 AM
To: interest at qt-project.org
Subject: Re: [Interest] Python staticmethod slot
On 2/4/20 10:16 PM, Jérôme Godbout wrote:
> Hi,
>
> Since the doc is pretty slim, and I have no clue how to make this
> work, anyone known how to call a staticmethod slot into PySide2 from Qml?
>
> This do work:
>
> @Slot(QVector3D, result=bool)
>
> def myfct(self, pos: QVector3D) -> bool:
>
> # ...
>
> return True
>
> But this doesn't work:
>
> @staticmethod
>
> @Slot(QVector3D, result=bool)
>
> def myfct(pos: QVector3D) -> bool:
>
> # ...
>
> return True
What's the error you get?
I would like to know why "doesn't work".
> How does one can call this properly?!? Do I have to make a duplicated
> function wrapper with the self that call the function without self
> just for Slot exposure? Right now the only workaround is to do this ugly mess:
>
> @staticmethod
>
> def myfct(pos: QVector3D) -> bool:
>
> return True
>
> @Slot(QVector3D, result=bool)
>
> def myfct_qml(self, pos: QVector3D) -> bool:
>
> “””Use _qml to change name since I cannot have the same name”””
>
> return MyClass.myfct(pos)
Yes, that doesn't look good.
Trying a small hello world does work though:
class MyWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton("Click me!")
self.layout = QVBoxLayout()
self.layout.addWidget(self.button)
self.setLayout(self.layout)
# Connecting the signal
self.button.clicked.connect(self.magic)
@staticmethod
@Slot()
def magic(self):
print("hello")
> I really hate to have to do this and having to create an Object
> Instance on top of it to call the function. Make it hard to have some
> Static Helper function (Q_INVOKABLE equivalent of C++) expose to Qml engine.
> This add up to the lack of qmlRegisterSingletonType. This is getting
> pretty ugly for simple function exposure.
Are you trying to get some "self.something" inside the slot?
because that will not work.
Let me know why it doesn't work,
and we can continue the discussion.
Cheers
--
Dr. Cristian Maureira-Fredes
R&D Manager
The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin
Geschäftsführer: Mika Pälsi,
Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B
--
_______________________________________________
Interest mailing list
Interest at qt-project.org
https://lists.qt-project.org/listinfo/interest
More information about the Interest
mailing list