{"id":2115,"date":"2016-09-11T07:21:32","date_gmt":"2016-09-11T05:21:32","guid":{"rendered":"https:\/\/www.opengis.ch\/?p=2115"},"modified":"2020-04-29T16:05:13","modified_gmt":"2020-04-29T14:05:13","slug":"updating-pyqt-signals-that-use-lambda-in-qgis-with-2to3","status":"publish","type":"post","link":"https:\/\/www.opengis.ch\/it\/2016\/09\/11\/updating-pyqt-signals-that-use-lambda-in-qgis-with-2to3\/","title":{"rendered":"Updating PyQt signals that use lambda in QGIS with 2to3"},"content":{"rendered":"<p>Just for the sake of documenting things, when running qgis 2to3 on a plugin I encountered a tricky situation regarding signals.<\/p>\n<pre class=\"lang:sh decode:true\" title=\"the 2to3 call\">MYQGISDIR\/scripts\/2to3 -f signals -w my\/plugin\/path<\/pre>\n<p>The original code:<\/p>\n<pre class=\"lang:python decode:true\" title=\"The original code\">extra_arg = \"my test argument\"\nQObject.connect(\n  action,\n  SIGNAL( \"triggered()\"),\n  lambda extra_arg=my_arg: show_extra_arg(extra_arg))\ndef do_load_project(extra_arg):\n  print extra_arg # \"my test argument\"\n<\/pre>\n<p>The generated code:<\/p>\n<pre class=\"lang:python decode:true\" title=\"The generated code\">extra_arg = \"test_arg\"\naction.triggered.connect(\n  lambda extra_arg=my_arg: show_extra_arg(extra_arg))\ndef do_load_project(extra_arg):\n  print extra_arg # False<\/pre>\n<p>so in <em>do_load_project<\/em> we get False instead of <em>&#8220;my test argument&#8221;<\/em>, why?<br \/>\nwell due to a subtle difference in the generated code. in the original code we had the signature <em>triggered()<\/em>\u00a0which has no arguments, so in our lambda <em>extra_arg<\/em>\u00a0gets passed <em>my_arg<\/em>.<br \/>\nin the generated code,\u00a0<em>triggered<\/em>\u00a0actually has an optional param <em>checked<\/em> [1] which if emitted gets passed to <em>extra_arg<\/em>\u00a0causing the problem.<br \/>\nThe correct code (note the additional argument in the lambda definition)<\/p>\n<pre class=\"lang:default decode:true\" title=\"The correct code (note the additional argument in the lambda definition)\">extra_arg = \"test_arg\"\naction.triggered.connect(\n  lambda checked, extra_arg=my_arg: show_extra_arg(extra_arg))\ndef do_load_project(extra_arg):\n  print extra_arg # False<\/pre>\n<p>some reference:<br \/>\n[0] <a href=\"https:\/\/pyqt.sourceforge.net\/Docs\/PyQt5\/signals_slots.html\">https:\/\/pyqt.sourceforge.net\/Docs\/PyQt5\/signals_slots.html<\/a><br \/>\n[1] <a href=\"https:\/\/doc.qt.io\/qt-4.8\/qaction.html#triggered\">https:\/\/doc.qt.io\/qt-4.8\/qaction.html#triggered<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just for the sake of documenting things, when running qgis 2to3 on a plugin I encountered a tricky situation regarding signals. MYQGISDIR\/scripts\/2to3 -f signals -w my\/plugin\/path The original code: extra_arg = &#8220;my test argument&#8221; QObject.connect( action, SIGNAL( &#8220;triggered()&#8221;), lambda extra_arg=my_arg: show_extra_arg(extra_arg)) def do_load_project(extra_arg): print extra_arg # &#8220;my test argument&#8221; The [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[37,16],"tags":[125],"class_list":["post-2115","post","type-post","status-publish","format-standard","hentry","category-pyqt","category-qgis-plugins","tag-qgis-org"],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":1668,"url":"https:\/\/www.opengis.ch\/it\/2016\/09\/07\/using-threads-in-qgis-python-plugins\/","url_meta":{"origin":2115,"position":0},"title":"Using threads in QGIS python plugins","author":"Marco Bernasocchi","date":"7 Settembre 2016","format":false,"excerpt":"Here an example on how to work with threads in a consistent and clean manner in QGIS python plugins","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2127,"url":"https:\/\/www.opengis.ch\/it\/2016\/09\/19\/qgis2-compatibility-plugin\/","url_meta":{"origin":2115,"position":1},"title":"QGIS2 compatibility plugin","author":"Marco Bernasocchi","date":"19 Settembre 2016","format":false,"excerpt":"Lately I've been spending time porting a bigger plugin from QGIS 2.8 to 3 while maintaining 2.8 compatibility. You can find it at https:\/\/github.com\/opengisch\/qgis2compat\/ and https:\/\/plugins.qgis.org\/plugins\/qgis2compat\/ One code to rule them all. My target was to have to edit the source code as little as possible to simulate a lazy\u2026","rel":"","context":"In &quot;PyQt&quot;","block_context":{"text":"PyQt","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/python\/pyqt\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3624,"url":"https:\/\/www.opengis.ch\/it\/2018\/06\/22\/threads-in-pyqgis3\/","url_meta":{"origin":2115,"position":2},"title":"Using Threads in PyQGIS3","author":"Marco Bernasocchi","date":"22 Giugno 2018","format":false,"excerpt":"While porting a plugin to QGIS3 I decided to also move all it's threading infrastructure to QgsTasks. Here three possible variants to implement this.the first uses the static method QgsTask.fromFunction and is simpler to use. A great quick solution. If you want need control you can look at the second\u2026","rel":"","context":"In &quot;PyQt&quot;","block_context":{"text":"PyQt","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/python\/pyqt\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.opengis.ch\/wp-content\/uploads\/2018\/06\/pexels-wendy-van-zyl-1212179-scaled.jpg?fit=1200%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.opengis.ch\/wp-content\/uploads\/2018\/06\/pexels-wendy-van-zyl-1212179-scaled.jpg?fit=1200%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.opengis.ch\/wp-content\/uploads\/2018\/06\/pexels-wendy-van-zyl-1212179-scaled.jpg?fit=1200%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.opengis.ch\/wp-content\/uploads\/2018\/06\/pexels-wendy-van-zyl-1212179-scaled.jpg?fit=1200%2C800&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.opengis.ch\/wp-content\/uploads\/2018\/06\/pexels-wendy-van-zyl-1212179-scaled.jpg?fit=1200%2C800&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3691,"url":"https:\/\/www.opengis.ch\/it\/2018\/04\/13\/porting-qgis-plugins-to-api-v3-strategy-and-tools\/","url_meta":{"origin":2115,"position":3},"title":"Porting QGIS plugins to API v3 &#8211; Strategy and tools","author":"Marco Bernasocchi","date":"13 Aprile 2018","format":false,"excerpt":"The Release of QGIS 3.0 was a great success and with the first LTR (3.4) scheduled for release this fall, it is now the perfect time to port your plugins to the new API. QGIS 3.0 is the first major release since September 2013 when QGIS 2.0 was released. During\u2026","rel":"","context":"In &quot;Featured&quot;","block_context":{"text":"Featured","link":"https:\/\/www.opengis.ch\/it\/category\/featured\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3314,"url":"https:\/\/www.opengis.ch\/fr\/2017\/10\/03\/cours-pyqgis-13-11-14-11-2017-a-neuchatel\/","url_meta":{"origin":2115,"position":4},"title":"Cours PyQGIS 13.11.\/14.11.2017 \u00e0 Neuch\u00e2tel","author":"Matthias Kuhn","date":"3 Ottobre 2017","format":false,"excerpt":"Le cours est complet. Le cours est destin\u00e9 aux utilisateurs avanc\u00e9s de QGIS qui souhaitent accro\u00eetre leurs possibilit\u00e9s gr\u00e2ce \u00e0 l\u2019utilisation de python dans QGIS. Lors de cette formation, nous aborderons diff\u00e9rentes possibilit\u00e9s d\u2019interaction avec l\u2019API QGIS ainsi que la cr\u00e9ation d\u2019interfaces graphiques simples avec PyQt. Les th\u00e8mes suivants seront\u2026","rel":"","context":"In &quot;QGIS&quot;","block_context":{"text":"QGIS","link":"https:\/\/www.opengis.ch\/fr\/category\/gis-fr\/qgis-fr\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2074,"url":"https:\/\/www.opengis.ch\/it\/2016\/05\/04\/qgis-qt5-and-python3-migration-current-state\/","url_meta":{"origin":2115,"position":5},"title":"QGIS: Qt5 and Python3 migration, current state","author":"Matthias Kuhn","date":"4 Maggio 2016","format":false,"excerpt":"Behind the scenes a lot has happened to get ready for Qt5 and Python3. On the same codebase that is becoming the next release QGIS 2.16. This is really a great thing since we can focus work on a single master branch and I'm very happy that we got so\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/cpp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_shortlink":"https:\/\/wp.me\/pbdBtI-y7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/2115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/comments?post=2115"}],"version-history":[{"count":1,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/2115\/revisions"}],"predecessor-version":[{"id":11141,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/2115\/revisions\/11141"}],"wp:attachment":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/media?parent=2115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/categories?post=2115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/tags?post=2115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}