1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Chapter 26. Gtk::Builder</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="sec-memory-shared-resources.html" title="Shared resources">
<link rel="next" href="sec-builder-accessing-widgets.html" title="Accessing widgets">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 26. Gtk::Builder</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-memory-shared-resources.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-builder-accessing-widgets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-builder"></a>Chapter 26. Gtk::Builder</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-builder.html#sec-builder-loading-ui-file">Loading the .ui file</a></span></li>
<li><span class="section"><a href="sec-builder-accessing-widgets.html">Accessing widgets</a></span></li>
<li><span class="section"><a href="sec-builder-using-derived-widgets.html">Using derived widgets</a></span></li>
</ul>
</div>
<p>
Although you can use C++ code to instantiate and arrange widgets, this can soon
become tedious and repetitive. And it requires a recompilation to show changes.
The <a class="ulink" href="https://gitlab.gnome.org/jpu/cambalache" target="_top">Cambalache</a>
application allows you to layout widgets on screen and then save an XML description
of the arrangement. Your application can then use the <span class="application">Gtk::Builder</span>
API to load that XML file at runtime and obtain a pointer to specifically named
widget instances.
</p>
<p>
This has the following advantages:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">Less C++ code is required.</li>
<li class="listitem">UI changes can be seen more quickly, so UIs are able to improve.</li>
<li class="listitem">Designers without programming skills can create and edit UIs.</li>
</ol></div>
<p>
</p>
<p>
You still need C++ code to deal with User Interface changes triggered by user
actions, but using <span class="application">Gtk::Builder</span> for the widget
layout allows you to focus on implementing that functionality.
</p>
<p>
<span class="application">Cambalache</span> replaces the <span class="application">Glade</span>
application. <span class="application">Glade</span> can generate XML files to be used
with gtk3/<span class="application">gtkmm</span>3, but it does not support gtk4/<span class="application">gtkmm</span>4.
</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-builder-loading-ui-file"></a>Loading the .ui file</h2></div></div></div>
<p>
<code class="classname">Gtk::Builder</code> must be used via a
<code class="classname">Glib::RefPtr</code>. Like all such classes, you need to use a
<code class="methodname">create()</code> method to instantiate it. For instance,</p>
<pre class="programlisting"><code class="code">auto builder = Gtk::Builder::create_from_file("basic.ui");
</code></pre>
<p>This will instantiate the windows defined in the <code class="filename">.ui</code> file.
</p>
<p>To instantiate just one window, or just one of the child widgets, you can specify the name of a widget as the second parameter. For instance,
</p>
<pre class="programlisting"><code class="code">auto builder = Gtk::Builder::create_from_file("basic.ui", "treeview_products");
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-memory-shared-resources.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-builder-accessing-widgets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Shared resources </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Accessing widgets</td>
</tr>
</table>
</div>
</body>
</html>
|