Skip to content

Commit

Permalink
Add a few more example of pyqtSlot() usage, inspiration from python-q…
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebird75 committed Apr 26, 2022
1 parent e296cfc commit b034933
Showing 1 changed file with 81 additions and 4 deletions.
85 changes: 81 additions & 4 deletions tests/pyqtslot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from PyQt5.QtCore import pyqtSlot
from typing import Union

from PyQt5.QtCore import pyqtSlot, QObject

@pyqtSlot(str)
def func_none(s: str) -> None:
Expand All @@ -8,9 +10,84 @@ def func_none(s: str) -> None:
def func_int(s: str) -> int:
return 42

@pyqtSlot(str, result='int')
@pyqtSlot(str, result='int') # this means int here
def func_str(s: str) -> str:
return '42'

func_none("test")
x = func_int("test") # type: int
@pyqtSlot(str, name='toto', revision=33, result=int)
def func_toto(s: str) -> int:
return 33

n = None
n = func_none("test")

i = 0
i = func_int("test")

s = ''
s = func_str('test')

print( func_toto('123') )

### Other example extracted from pyqtSlot documentation

class Foo(QObject):

@pyqtSlot()
def foo_noarg(self) -> None:
""" C++: void foo() """
pass

@pyqtSlot(int, str)
def foo_int_str(self, arg1: int, arg2: str) -> None:
""" C++: void foo(int, QString) """
pass

@pyqtSlot(int, name='bar')
def foo_int(self, arg1: int) -> None:
""" C++: void bar(int) """
pass

@pyqtSlot(int, result=int)
def foo_int2(self, arg1: int) -> int:
""" C++: int foo(int) """
pass

# make sure that the return type is type-checked
@pyqtSlot(int, result=int) # type: ignore[arg-type]
def foo_int_with_return_arg_error(self, arg1: int) -> str:
""" C++: int foo(int) """
pass

# mypy can not typecheck the pyqtSlot() arguments vs method arguments
@pyqtSlot(int, result=int)
def foo_int_with_arg_error(self, arg1: str) -> int:
""" C++: int foo(int) """
pass

@pyqtSlot(int, QObject)
def foo_qobject(self, arg1: QObject) -> None:
""" C++: int foo(int, QObject *) """
pass

@pyqtSlot(int)
@pyqtSlot('QString')
def valueChanged(self, value: Union[int, str]) -> None:
""" Two slots will be defined in the QMetaObject. """
pass


f = Foo()

n = f.foo_noarg()

n = f.foo_int_str(33, 'abc')

n = f.foo_int(33)

i = f.foo_int2(33)

n = f.foo_qobject(QObject())

n = f.valueChanged(33)
n = f.valueChanged('abc')

0 comments on commit b034933

Please sign in to comment.