[Interest] Python staticmethod slot

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Wed Feb 5 15:42:09 CET 2020



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
--


More information about the Interest mailing list