Debian Codegraph @

docs/tutorial/html/sec-overriding-default-signal-handlers.html

raw
1 <html>
2 <head>
3 <meta charset="UTF-8">
4 <link rel="stylesheet" href="highlight.min.css">
5 <script src="highlight.min.js"></script><script>
6 hljs.configure({languages: ['cpp']});
7 hljs.highlightAll();
8 </script><title>Overriding default signal handlers</title>
9 <link rel="stylesheet" type="text/css" href="style.css">
10 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
11 <link rel="home" href="index.html" title="Programming with gtkmm 4">
12 <link rel="up" href="chapter-signals.html" title="Appendix B. Signals">
13 <link rel="prev" href="sec-disconnecting-signal-handlers.html" title="Disconnecting signal handlers">
14 <link rel="next" href="sec-binding-extra-arguments.html" title="Binding extra arguments">
15 </head>
16 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
17 <div class="navheader">
18 <table width="100%" summary="Navigation header">
19 <tr><th colspan="3" align="center">Overriding default signal handlers</th></tr>
20 <tr>
21 <td width="20%" align="left">
22 <a accesskey="p" href="sec-disconnecting-signal-handlers.html"><img src="icons/prev.png" alt="Prev"></a> </td>
23 <th width="60%" align="center">Appendix B. Signals</th>
24 <td width="20%" align="right"> <a accesskey="n" href="sec-binding-extra-arguments.html"><img src="icons/next.png" alt="Next"></a>
25 </td>
26 </tr>
27 </table>
28 <hr>
29 </div>
30 <div class="section">
31 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
32 <a name="sec-overriding-default-signal-handlers"></a>Overriding default signal handlers</h2></div></div></div>
33
34
35 <p>
36 So far we've told you to perform actions in
37 response to button-presses and the like by handling signals.
38 That's certainly a good way to do things, but it's not the only
39 way.
40 </p>
41
42 <p>
43 Instead of laboriously connecting signal handlers to signals,
44 you can simply make a new class which inherits from a widget - say, a
45 Button - and then override the default signal handler, such as Button::on_clicked(). This can be a
46 lot simpler than hooking up signal handlers for everything.
47 </p>
48
49 <p>
50 Subclassing isn't always the best way to accomplish
51 things. It is only useful when you want the widget to handle its own signal by itself. If you want some other class to handle the signal then you'll need to connect a separate handler. This is even more true if you want several objects to handle the same signal, or if you want one signal handler to respond to the same signal from different objects.
52 </p>
53
54 <p>
55 <span class="application">gtkmm</span> classes are designed with overriding in mind; they contain
56 virtual member methods specifically intended to be overridden.
57 </p>
58
59 <p>
60 Let's look at an example of overriding:
61 </p>
62
63 <pre class="programlisting"><code class="code">#include &lt;gtkmm/button.h&gt;
64
65 class OverriddenButton : public Gtk::Button
66 {
67 protected:
68 void on_clicked() override;
69 }
70
71 void OverriddenButton::on_clicked()
72 {
73 std::cout &lt;&lt; "Hello World" &lt;&lt; std::endl;
74
75 // call the base class's version of the method:
76 Gtk::Button::on_clicked();
77 }
78 </code></pre>
79
80 <p>
81 Here we define a new class called <code class="classname">OverriddenButton</code>,
82 which inherits from <code class="classname">Gtk::Button</code>. The only thing we
83 change is the <code class="methodname">on_clicked()</code> method, which is called
84 whenever <code class="classname">Gtk::Button</code> emits the
85 <code class="literal">clicked</code> signal. This method prints "Hello World" to
86 <code class="literal">stdout</code>, and then calls the original, overridden method, to
87 let <code class="classname">Gtk::Button</code> do what it would have done had we not
88 overridden.
89 </p>
90
91 <p>
92 You don't always need to call the parent's method; there are times
93 when you might not want to. Note that we called the parent method
94 <span class="emphasis"><em>after</em></span> writing "Hello World", but we could have called it before.
95 In this simple example, it hardly matters much, but there are times
96 when it will. With connected signal handlers, it's not quite so easy to change details
97 like this, and you can do something here which you can't do at all
98 with connected signal handlers: you can call the parent method in the <span class="emphasis"><em>middle</em></span> of
99 your custom code.
100 </p>
101
102 </div>
103 <div class="navfooter">
104 <hr>
105 <table width="100%" summary="Navigation footer">
106 <tr>
107 <td width="40%" align="left">
108 <a accesskey="p" href="sec-disconnecting-signal-handlers.html"><img src="icons/prev.png" alt="Prev"></a> </td>
109 <td width="20%" align="center"><a accesskey="u" href="chapter-signals.html"><img src="icons/up.png" alt="Up"></a></td>
110 <td width="40%" align="right"> <a accesskey="n" href="sec-binding-extra-arguments.html"><img src="icons/next.png" alt="Next"></a>
111 </td>
112 </tr>
113 <tr>
114 <td width="40%" align="left" valign="top">Disconnecting signal handlers </td>
115 <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
116 <td width="40%" align="right" valign="top"> Binding extra arguments</td>
117 </tr>
118 </table>
119 </div>
120 </body>
121 </html>