<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- worldtimeclockbuilder.qdoc --> <title>World Time Clock Builder Example | Qt Designer Manual</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtdesigner-manual.html">Qt Designer Manual</a></td><td ><a href="examples-designer.html">Qt Designer Examples</a></td><td >World Time Clock Builder Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#preparation">Preparation</a></li> <li class="level1"><a href="#loading-and-building-the-form">Loading and Building the Form</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">World Time Clock Builder Example</h1> <span class="subtitle"></span> <!-- $$$worldtimeclockbuilder-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/worldtimeclockbuilder-example.png" alt="" /></p><p>This example uses a form containing the custom widget plugin described in the <a href="qtdesigner-worldtimeclockplugin-example.html">World Time Clock Plugin</a> example, and dynamically generates a user interface using the QUiLoader class, part of the QtUiTools module.</p> <a name="preparation"></a> <h2 id="preparation">Preparation</h2> <p>As with the <a href="qtdesigner-calculatorbuilder-example.html">Calculator Builder</a> example, the project file for this example needs to include the appropriate definitions to ensure that it is built against the required Qt modules.</p> <pre class="cpp"> QT += widgets uitools SOURCES = main.cpp RESOURCES = worldtimeclockbuilder.qrc </pre> <p>By appending <code>form</code> to the <code>CONFIG</code> declaration, we instruct <code>qmake</code> to generate a dependency on the <code>libQtUiTools</code> library containing the QtUiTools classes.</p> <p>Note that we do not inform <code>qmake</code> about any UI files, and so none will be processed and built into the application. The resource file contains an entry for the particular form that we wish to use:</p> <pre class="cpp"> <!DOCTYPE RCC><RCC version="1.0"> <qresource prefix="/forms"> <file>form.ui</file> </qresource> </RCC> </pre> <p>Forms do not need to be included with the application in this way. We only include a form in the application's resources for convenience, and to keep the example short.</p> <a name="loading-and-building-the-form"></a> <h2 id="loading-and-building-the-form">Loading and Building the Form</h2> <p>Since this example only loads and displays a pre-prepared form, all of the work can be done in the main() function. We are using a class from the QtUiTools library so, in addition to any other Qt classes that are normally required to write an application, we must include the appropriate header file:</p> <pre class="cpp"> <span class="preprocessor">#include <QtUiTools></span> </pre> <p>The main function initializes the resource system with the Q_INIT_RESOURCE() macro and constructs an QApplication instance in the usual way:</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { Q_INIT_RESOURCE(worldtimeclockbuilder); <span class="type">QApplication</span> app(argc<span class="operator">,</span> argv); <span class="type">QUiLoader</span> loader; </pre> <p>We construct a QUiLoader object to handle the form we want to use.</p> <p>The form itself is obtained from the resource file system using the path defined in the resource file. We use the form loader to load and construct the form:</p> <pre class="cpp"> <span class="type">QFile</span> file(<span class="string">":/forms/form.ui"</span>); file<span class="operator">.</span>open(<span class="type">QFile</span><span class="operator">::</span>ReadOnly); <span class="type">QWidget</span> <span class="operator">*</span>widget <span class="operator">=</span> loader<span class="operator">.</span>load(<span class="operator">&</span>file); file<span class="operator">.</span>close(); widget<span class="operator">-</span><span class="operator">></span>show(); </pre> <p>Once the form has been loaded, the resource file can be closed and the widget is shown.</p> <pre class="cpp"> <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <p>The form loader ensures that all the signal and slot connections between objects in the form are set up correctly when the form is loaded. As a result, the time is updated by the World Time Clock widget, and the time zone spin box can be used to change the position of the hour hand.</p> <p>Files:</p> <ul> <li><a href="qtdesigner-worldtimeclockbuilder-form-ui.html">worldtimeclockbuilder/form.ui</a></li> <li><a href="qtdesigner-worldtimeclockbuilder-main-cpp.html">worldtimeclockbuilder/main.cpp</a></li> <li><a href="qtdesigner-worldtimeclockbuilder-worldtimeclockbuilder-pro.html">worldtimeclockbuilder/worldtimeclockbuilder.pro</a></li> <li><a href="qtdesigner-worldtimeclockbuilder-worldtimeclockbuilder-qrc.html">worldtimeclockbuilder/worldtimeclockbuilder.qrc</a></li> </ul> </div> <!-- @@@worldtimeclockbuilder --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>