From 0cae15c6fa60572b623b2c09441b3a2a2609a4aa Mon Sep 17 00:00:00 2001
From: Matthias Koefferlein [virtual] bool event(QEvent ptr arg1): [virtual] bool event(QEvent ptr ev): A virtual method called "event" returning a boolean value
(Ruby "true" or "false") and expecting one argument (a pointer to a QEvent object).
"ptr" indicates that the argument is a pointer, "arg1" is the argument name.
@@ -108,9 +108,12 @@
An iterator called "each_reference" delivering RdbReference objects. [event] void layoutAboutToBeChanged: A parameterless event called "layoutAboutToBeChanged".
- [signal] void layoutAboutToBeChanged: A parameterless signal (event) called "layoutAboutToBeChanged" (see for details about events or signals). [signal] void objectNameChanged(string objectName): A signal (event) called "objectNameChanged" with one string argument.
diff --git a/src/doc/doc/programming/events.xml b/src/doc/doc/programming/events.xml
index 827102a7c..044ea331b 100644
--- a/src/doc/doc/programming/events.xml
+++ b/src/doc/doc/programming/events.xml
@@ -59,10 +59,12 @@
- Here is the code: + Here is the code. + This example demonstrates how the "get" method is reimplemented to deliver the actual text.
-module MyMacro ++ + The Python version is this: + ++module MyMacro include RBA @@ -78,15 +80,32 @@ dialog.home = "int:0" dialog.exec -end+end +
+from pya import BrowserSource, BrowserDialog
+
+class MyBrowserSource(BrowserSource):
+ def get(self, url):
+ next_url = "int:" + str(int(url.split(":")[1]) + 1)
+ return f"This is {url}. <a href='{next_url}'>Goto next ({next_url})</a>>"
+
+dialog = BrowserDialog()
+dialog.home = "int:0"
+dialog.source = MyBrowserSource()
+dialog.exec_()
+
- This example demonstrates how the "get" method is reimplemented to deliver the actual text. Ruby even allows reimplementation of a method without deriving a new class, because it allows - to define methods per instance: + defining methods per instance:
-module MyMacro ++end ++module MyMacro include RBA @@ -101,7 +120,8 @@ enddialog.home = "int:0" dialog.exec -end
module MyMacro ++end + + ++module MyMacro include RBA @@ -129,14 +150,33 @@ endApplication::instance.main_window.menu.insert_item("@toolbar.end", "my_action", action) -end
+ The Python version is: +
+ +
+from pya import Action, MessageBox, Application
+
+def on_triggered():
+ MessageBox.info("A message", "The action was triggered", MessageBox.Ok)
+
+action = Action()
+action.on_triggered = on_triggered
+action.title = "My Action"
+
+Application.instance().main_window().menu().insert_item("@toolbar.end", "my_action", action)
+
Specifying a block to an event will make the event only execute that block. A more flexible way of controlling the code attached to events is available through the += and -= operators:
-module MyMacro ++ + ++module MyMacro include RBA @@ -157,7 +197,12 @@ end# to clear all event handlers use: action.on_triggered.clear -
+ Synonyms for the += operator are add and connect. The latter makes code more familiar for PyQt users. + In the same way, synonyms for the -= operator are remove and disconnect. +
If the Qt binding is available (see ), Qt signals @@ -166,7 +211,8 @@ end input field to the label below:
-module MyMacro ++end + + ++module MyMacro include RBA @@ -182,13 +228,38 @@ enddialog.exec -end
+ The Python version is: +
+ ++from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application + +dialog = QDialog(Application.instance().main_window()) +layout = QVBoxLayout(dialog) +input = QLineEdit(dialog) +label = QLabel(dialog) +layout.addWidget(input) +layout.addWidget(label) + +def text_changed(text): + label.text = text + +# implement the textChanged signal as event: +input.textChanged = text_changed + +dialog.exec_() +
Using the += operator on the event, multiple handlers can be added to a signal:
-module MyMacro ++end + + ++module MyMacro include RBA @@ -207,7 +278,37 @@ enddialog.exec -end
+ with the Python version: +
+ ++from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application + +dialog = QDialog(Application.instance().main_window()) +layout = QVBoxLayout(dialog) +input = QLineEdit(dialog) +label1 = QLabel(dialog) +label2 = QLabel(dialog) +layout.addWidget(input) +layout.addWidget(label1) +layout.addWidget(label2) + +def text_changed1(text): + label1.text = text + +def text_changed2(text): + label2.text = text[::-1] + +# two signal consumers: +input.textChanged += text_changed1 +input.textChanged += text_changed2 + +dialog.exec_() +