{"id":93,"date":"2012-04-01T21:56:40","date_gmt":"2012-04-01T21:56:40","guid":{"rendered":"http:\/\/www.arnorehn.de\/blog\/?p=93"},"modified":"2016-08-07T11:59:39","modified_gmt":"2016-08-07T09:59:39","slug":"connecting-c11-lambdas-to-qt4-signals-with-libffi","status":"publish","type":"post","link":"https:\/\/www.arnorehn.de\/blog\/2012\/04\/01\/connecting-c11-lambdas-to-qt4-signals-with-libffi\/","title":{"rendered":"Connecting C++11 lambdas to Qt4 Signals with libffi (now portable)"},"content":{"rendered":"<p>I know that this is probably not very useful, <del>considering that it&#8217;s not portable and<\/del>\u00a0with Qt5 about to be <a href=\"http:\/\/qt-project.org\/wiki\/Qt_5.0\">released in June<\/a>, but I found it interesting to experiment with the new language features and libffi.<\/p>\n<p>The result is a class which lets you connect any function pointer or lambda, with or without bound variables, to Qt4 signals. Example of what you can do with it:<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;QtCore&gt;\r\n#include &lt;QtGui&gt;\r\n#include &lt;QtDebug&gt;\r\n\r\n#include \"lambdaconnection.h\"\r\n\r\nvoid printValue(int i) {\r\n    qDebug() &lt;&lt; \"value:\" &lt;&lt; i;\r\n}\r\n\r\nint main(int argc, char **argv) {\r\n    QApplication app(argc, argv);\r\n\r\n    QSlider slider(Qt::Horizontal);\r\n    slider.show();\r\n\r\n    LambdaConnection::connect(&amp;slider, SIGNAL(valueChanged(int)), &amp;printValue);\r\n\r\n    LambdaConnection::connect(&amp;slider, SIGNAL(valueChanged(int)), [&amp;app] (int i)\r\n    {\r\n        if (i &gt;= 99) {\r\n            qDebug() &lt;&lt; \"quit.\";\r\n            app.quit();\r\n        }\r\n    }); \r\n\r\n    app.exec();\r\n}<\/pre>\n<p>Quite nice, I think\u00a0<img decoding=\"async\" src=\"http:\/\/www.arnorehn.de\/blog\/wp-includes\/images\/smilies\/icon_smile.gif\" alt=\":)\" \/>\u00a0You can get the source for\u00a0<a href=\"http:\/\/www.arnorehn.de\/blog\/wp-content\/uploads\/2012\/04\/lambdaconnection.h\">lambdaconnection.h here<\/a>.<\/p>\n<p><del>Its main drawback is the use of pointers to member functions as ordinary function pointers. It works with gcc and maybe clang, but it\u2019s probably going to break with other compilers and\/or operating systems.<\/del>\u00a0<strong>UPDATE:<\/strong>\u00a0By converting everything to std::function, we can have one template function which is called by libffi and handles any further lambda calls. No need for member-function-pointers anymore. Portability shouldn&#8217;t be an issue anymore, too.<\/p>\n<p>Additionally, to avoid code duplication, it simply converts function pointers to std::function objects. This adds some overhead when calling the function pointer.<\/p>\n<p>Anyway, it\u2019s only an afternoon\u2019s work and it was fun to code. Maybe someone will find this interesting\u00a0<img decoding=\"async\" src=\"http:\/\/www.arnorehn.de\/blog\/wp-includes\/images\/smilies\/icon_smile.gif\" alt=\":)\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know that this is probably not very useful, considering that it&#8217;s not portable and\u00a0with Qt5 about to be released in June, but I found it interesting to experiment with the new language features and libffi. The result is a class which lets you connect any function pointer or lambda, with or without bound variables, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[21,23,20,22],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-qt","tag-c11","tag-lambda","tag-libffi","tag-qt"],"_links":{"self":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":14,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/160"}],"wp:attachment":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}