{"id":4,"date":"2005-09-16T00:13:48","date_gmt":"2005-09-16T04:13:48","guid":{"rendered":"http:\/\/thesmithfam.org\/blog\/?p=4"},"modified":"2019-08-12T07:16:43","modified_gmt":"2019-08-12T13:16:43","slug":"qt-connecting-signals-to-signals","status":"publish","type":"post","link":"https:\/\/thesmithfam.org\/blog\/2005\/09\/16\/qt-connecting-signals-to-signals\/","title":{"rendered":"Qt: Connecting signals to &#8230; signals"},"content":{"rendered":"<p>I found out today that Qt&#8217;s slots\/signals architecture is even better than I thought. Normally, developers connect widget <b>signals<\/b> to widget <b>slots<\/b> to be notified of events. Today I discovered that signals can actually be connected to other signals, which saved me from writing some really stupid code&#8230;<\/p>\n<p>This may seem weird, but consider this scenario. You have a custom widget, called <code>MyWidget<\/code>, that has a signal, <code>somethingClicked()<\/code>, which is emitted when a user clicks, uh, something. But now you want to add a <code>QPopupMenu<\/code> as a right-click context menu, that causes the  <code>somethingClicked()<\/code> signal to be emitted also. It turns out there&#8217;s a hard way and an easy way to do this.<\/p>\n<p><strong>First, the hard way:<\/strong><\/p>\n<p>1. Add a new slot called <code>emitSomethingClickedSlot()<\/code> that looks like this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nMyWidget::emitSomethingClickedSlot() {\r\n        emit somethingClicked();\r\n}\r\n<\/pre>\n<p>2. Then, connect the <code>QPopupMenu<\/code>&#8216;s menu item to this slot, like so:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npopupMenu-&gt;insertItem( &quot;Click me&quot;, this,\r\n     SLOT(emitSomethingClickedSlot()) );\r\n<\/pre>\n<p>That sucks. We just created a slot whose sole purpose is to turn around and emit a signal. What a waste of editor space. It would have been smarter to connect the menu item&#8217;s signal directly to the <code>somethingClicked()<\/code> signal.<\/p>\n<p><strong>Here&#8217;s the easy way:<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npopupMenu-&gt;insertItem( &quot;Click me&quot;, this,\r\n     SIGNAL(somethingClicked()) );\r\n<\/pre>\n<p>Now that&#8217;s concise. Sometimes it pays to connect signals to signals.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Qt allows you to connect signals to slots, but did you know that you can connect signals to other signals? This makes it easier in some cases to move events around.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-code-and-cruft"],"_links":{"self":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":2,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":1629,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/4\/revisions\/1629"}],"wp:attachment":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}