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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Generating C++ Source Code From The Qt Designer File</title>
<meta name="generator" content="Adobe GoLive 4">
<link rel="HOME" href="book1.html">
<link rel="UP" title="Tutorial: Creating A Simple Application With The Help Of
Qt Designer" href="chap2_1.html">
<link rel="PREVIOUS" title="More Widgets For Our Form" href="chap2_2.html">
<link rel="NEXT" title="Putting It All Together" href="chap2_4.html">
</head>
<body class="SECT1" bgcolor="white" text="black" link="blue" vlink="#840084" alink="blue">
<div class="NAVHEADER">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="500" align="left" valign="middle"><img height="90" width="475" src="figures/title.png" border="0"></td>
<td></td>
<td align="right" valign="top"><a href="../company/about/trolls.html">
<table border="0" cellpadding="0" cellspacing="0" width="137">
<tr>
<td><img height="100" width="100" src="figures/f001.png" border="0"></td>
<td><img height="100" width="100" src="figures/qtlogo.png" align="top" border="0"></td>
</tr>
</table>
</a></td>
</tr>
<tr>
<td width="500"><spacer type="block" width="500" height="20"></td>
<td><spacer type="block" width="42" height="20"></td>
<td><br>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="black">
<tr>
<td width="10%" align="left" valign="bottom"><a href="chap2_2.html"><img height="20" width="20" src="arrows/left.png" border="0"></a></td>
<td width="80%" align="center" valign="bottom"><font color="white"><b>Chapter 2. Tutorial: Creating A Simple Application With The Help Of Qt Designer</b></font></td>
<td width="10%" align="right" valign="bottom"><a href="chap2_4.html"><img height="20" width="20" src="arrows/right.png" border="0"></a></td>
</tr>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1"><a name="AEN336"></a>Generating C++ Source Code From The Qt Designer File</h1>
<p>As you might have guessed from the filename extension <tt class="FILENAME">.ui</tt>, Qt Designer does not generate C++ source code right away. While this would be possible, it would make it much more difficult to change the dialogs later by using Qt Designer.</p>
<p>Out of curiosity, you might be wondering what it is that Qt Designer saves in its <tt class="FILENAME">.ui</tt> files. These files are a textual description of the dialog you just created. The format used is the markup language XML. Here is what <tt class="FILENAME">pizza.ui</tt> can look like; your file might have slightly different values for sizes and positions:</p>
<pre class="SCREEN"><!DOCTYPE UI><UI>
<class>PizzaEntry</class>
<widget>
<class>QDialog</class>
<property>
<name>name</name>
<cstring>PizzaEntry</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>0</x>
<y>0</y>
<width>310</width>
<height>222</height>
</rect>
</property>
<property>
<name>caption</name>
<string>Form1</string>
</property>
<widget>
<class>QButtonGroup</class>
<property>
<name>name</name>
<cstring>ButtonGroup1</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>30</x>
<y>10</y>
<width>122</width>
<height>119</height>
</rect>
</property>
<property>
<name>title</name>
<string>Toppings</string>
</property>
<widget>
<class>QCheckBox</class>
<property>
<name>name</name>
<cstring>CheckBox1</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>20</y>
<width>102</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Mushrooms</string>
</property>
</widget>
<widget>
<class>QCheckBox</class>
<property>
<name>name</name>
<cstring>CheckBox2</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>40</y>
<width>51</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Ham</string>
</property>
</widget>
<widget>
<class>QCheckBox</class>
<property>
<name>name</name>
<cstring>CheckBox3</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>60</y>
<width>88</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Pineapple</string>
</property>
</widget>
<widget>
<class>QCheckBox</class>
<property>
<name>name</name>
<cstring>CheckBox4</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>80</y>
<width>79</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Anchovies</string>
</property>
</widget>
</widget>
<widget>
<class>QButtonGroup</class>
<property>
<name>name</name>
<cstring>ButtonGroup2</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>180</x>
<y>20</y>
<width>100</width>
<height>99</height>
</rect>
</property>
<property>
<name>title</name>
<string>Size</string>
</property>
<widget>
<class>QRadioButton</class>
<property>
<name>name</name>
<cstring>RadioButton1</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>20</y>
<width>56</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Junior</string>
</property>
</widget>
<widget>
<class>QRadioButton</class>
<property>
<name>name</name>
<cstring>RadioButton2</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>40</y>
<width>71</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Standard</string>
</property>
</widget>
<widget>
<class>QRadioButton</class>
<property>
<name>name</name>
<cstring>RadioButton3</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>10</x>
<y>60</y>
<width>58</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Family</string>
</property>
</widget>
</widget>
<widget>
<class>QCheckBox</class>
<property>
<name>name</name>
<cstring>CheckBox5</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>110</x>
<y>150</y>
<width>101</width>
<height>19</height>
</rect>
</property>
<property>
<name>text</name>
<string>Extra Cheese</string>
</property>
</widget>
<widget>
<class>QPushButton</class>
<property>
<name>name</name>
<cstring>PushButton1_2_2</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>50</x>
<y>180</y>
<width>108</width>
<height>32</height>
</rect>
</property>
<property>
<name>text</name>
<string>OK</string>
</property>
</widget>
<widget>
<class>QPushButton</class>
<property>
<name>name</name>
<cstring>PushButton1_2</cstring>
</property>
<property>
<name>geometry</name>
<rect>
<x>170</x>
<y>180</y>
<width>108</width>
<height>32</height>
</rect>
</property>
<property>
<name>text</name>
<string>Cancel</string>
</property>
</widget>
</widget>
</UI></pre>
<p>The first few lines specify that this is a Qt Designer file and that it describes a dialog that should be called <span class="GUILABEL">PizzaEntry</span>. The rest describe the individual widgets by listing their names, their classes, and some of their properties. Only those properties that are different from the default—i.e., those values that were automatically set when you created the widget in the form—are listed here. You could in theory change values here, but this is not recommended, since you might break the format if you do not know exactly what you are doing. It's better to edit these files with Qt Designer.</p>
<p>Of course, no C++ compiler can use such a file, so you will have to convert it first. This is the task of another program that comes with Qt Designer: <b class="COMMAND">uic</b>, the user interface compiler.</p>
<p>You need to call <b class="COMMAND">uic</b> twice to generate both the header file and the implementation file of your dialog class. Assuming that <b class="COMMAND">uic</b> is in your search path, you can generate the header file as follows:</p>
<pre class="SCREEN">uic -o PizzaEntry.h pizza.ui</pre>
<p>The option <tt>-o</tt> tells <b class="COMMAND">uic</b> how to name the header file, and <tt class="FILENAME">pizza.ui</tt> is the name of the file you saved with Qt Designer. As with the XML file, let's have a brief look at the generated file:</p>
<pre class="SCREEN">/****************************************************************************
** Form interface generated from reading ui file 'pizza.ui'
**
** Created: Tue Jun 20 13:22:32 2000
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef PIZZAENTRY_H
#define PIZZAENTRY_H
#include <qdialog.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QButtonGroup;
class QCheckBox;
class QPushButton;
class QRadioButton;
class PizzaEntry : public QDialog
{
Q_OBJECT
public:
PizzaEntry( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags f = 0 );
~PizzaEntry();
QButtonGroup* ButtonGroup1;
QCheckBox* CheckBox1;
QCheckBox* CheckBox2;
QCheckBox* CheckBox3;
QCheckBox* CheckBox4;
QButtonGroup* ButtonGroup2;
QRadioButton* RadioButton1;
QRadioButton* RadioButton2;
QRadioButton* RadioButton3;
QCheckBox* CheckBox5;
QPushButton* PushButton1_2_2;
QPushButton* PushButton1_2;
protected:
};
#endif // PIZZAENTRY_H</pre>
<p>If you have programmed with Qt before, the only thing that might surprise you here is the fact that all the widget members of the dialog are made <tt class="LITERAL">public</tt>. We'll discuss later why this is so.</p>
<p>The next step is to create the implementation file. You tell <b class="COMMAND">uic</b> that you want to create an implementation file instead of a header file by specifying the option <tt class="OPTION">-i</tt> and passing the name of the already generated header file:</p>
<pre class="SCREEN">uic -i PizzaEntry.h -o PizzaEntry.cpp pizza.ui</pre>
<p>As before, the option <tt class="OPTION">-o</tt> determines the name of the generated file. Again, let's look at the result:</p>
<pre class="SCREEN">/****************************************************************************
** Form implementation generated from reading ui file 'pizza.ui'
**
** Created: Tue Jun 20 13:23:00 2000
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "PizzaEntry.h"
#include <qbuttongroup.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qradiobutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
/*
* Constructs a PizzaEntry which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
PizzaEntry::PizzaEntry( QWidget* parent, const char* name, bool modal, WFlags f )
: QDialog( parent, name, modal, f )
{
if ( !name )
setName( "PizzaEntry" );
resize( 310, 222 );
setCaption( tr( "Form1" ) );
ButtonGroup1 = new QButtonGroup( this, "ButtonGroup1" );
ButtonGroup1->setGeometry( QRect( 30, 10, 122, 119 ) );
ButtonGroup1->setTitle( tr( "Toppings" ) );
CheckBox1 = new QCheckBox( ButtonGroup1, "CheckBox1" );
CheckBox1->setGeometry( QRect( 10, 20, 102, 19 ) );
CheckBox1->setText( tr( "Mushrooms" ) );
CheckBox2 = new QCheckBox( ButtonGroup1, "CheckBox2" );
CheckBox2->setGeometry( QRect( 10, 40, 51, 19 ) );
CheckBox2->setText( tr( "Ham" ) );
CheckBox3 = new QCheckBox( ButtonGroup1, "CheckBox3" );
CheckBox3->setGeometry( QRect( 10, 60, 88, 19 ) );
CheckBox3->setText( tr( "Pineapple" ) );
CheckBox4 = new QCheckBox( ButtonGroup1, "CheckBox4" );
CheckBox4->setGeometry( QRect( 10, 80, 79, 19 ) );
CheckBox4->setText( tr( "Anchovies" ) );
ButtonGroup2 = new QButtonGroup( this, "ButtonGroup2" );
ButtonGroup2->setGeometry( QRect( 180, 20, 100, 99 ) );
ButtonGroup2->setTitle( tr( "Size" ) );
RadioButton1 = new QRadioButton( ButtonGroup2, "RadioButton1" );
RadioButton1->setGeometry( QRect( 10, 20, 56, 19 ) );
RadioButton1->setText( tr( "Junior" ) );
RadioButton2 = new QRadioButton( ButtonGroup2, "RadioButton2" );
RadioButton2->setGeometry( QRect( 10, 40, 71, 19 ) );
RadioButton2->setText( tr( "Standard" ) );
RadioButton3 = new QRadioButton( ButtonGroup2, "RadioButton3" );
RadioButton3->setGeometry( QRect( 10, 60, 58, 19 ) );
RadioButton3->setText( tr( "Family" ) );
CheckBox5 = new QCheckBox( this, "CheckBox5" );
CheckBox5->setGeometry( QRect( 110, 150, 101, 19 ) );
CheckBox5->setText( tr( "Extra Cheese" ) );
PushButton1_2_2 = new QPushButton( this, "PushButton1_2_2" );
PushButton1_2_2->setGeometry( QRect( 50, 180, 108, 32 ) );
PushButton1_2_2->setText( tr( "OK" ) );
PushButton1_2 = new QPushButton( this, "PushButton1_2" );
PushButton1_2->setGeometry( QRect( 170, 180, 108, 32 ) );
PushButton1_2->setText( tr( "Cancel" ) );
}
/*
* Destroys the object and frees any allocated resources
*/
PizzaEntry::~PizzaEntry()
{
// no need to delete child widgets, Qt does it all for us
}</pre>
<p>You might feel that this source code is a bit more verbose than handcrafted code, but this is always the price you pay for the convenience of machine-generated code.</p>
</div>
<div class="NAVFOOTER">
<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="black">
<tr>
<td width="33%" align="left" valign="top"><a href="chap2_2.html"><img height="20" width="20" src="arrows/left.png" border="0"></a></td>
<td width="34%" align="center" valign="top"><a href="book1.html"><img height="20" width="25" src="arrows/home.png" border="0"></a></td>
<td width="33%" align="right" valign="top"><a href="chap2_4.html"><img height="20" width="20" src="arrows/right.png" border="0"></a></td>
</tr>
<tr>
<td width="33%" align="left" valign="top"><font color="white">More Widgets For Our Form</font></td>
<td width="34%" align="center" valign="top"><a href="chap2_1.html"><img height="20" width="20" src="arrows/up.png" border="0"></a></td>
<td width="33%" align="right" valign="top"><font color="white">Putting It All Together</font></td>
</tr>
</table>
</div>
</body>
</html>
|