File: chap3_3.html

package info (click to toggle)
qt-embedded 2.3.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68,608 kB
  • ctags: 45,998
  • sloc: cpp: 276,654; ansic: 71,987; makefile: 29,074; sh: 12,305; yacc: 2,465; python: 1,863; perl: 481; lex: 480; xml: 68; lisp: 15
file content (90 lines) | stat: -rw-r--r-- 7,816 bytes parent folder | download | duplicates (3)
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
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Initializing The State Of Widgets In The Form</title>
		<meta name="generator" content="Adobe GoLive 4">
		<link rel="HOME" href="book1.html">
		<link rel="UP" title="Tutorial: Adding Functionality To A Dialog By
	  Subclassing" href="chap3_1.html">
		<link rel="PREVIOUS" title="Adding Functionality To The Push Buttons" href="chap3_2.html">
		<link rel="NEXT" title="Connecting Widgets To Each Other" href="chap3_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="chap3_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 3. Tutorial: Adding Functionality To A Dialog By Subclassing</b></font></td>
					<td width="10%" align="right" valign="bottom"><a href="chap3_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="AEN598"></a>Initializing The State Of Widgets In The Form</h1>
			<p>Our next goal is to initialize the widgets in the dialog. To repeat: We want all the toppings check boxes to be checked and the <span class="GUILABEL">Family</span> size to be chosen. And finally, we want to check the extra cheese check box on Mondays, but only on Mondays.</p>
			<p>Initialization is usually done in a constructor, and our pizza order-entry application is no different. As before, we will not touch the code generated by <b class="COMMAND">uic</b>, because this code would be lost the next time we run <b class="COMMAND">uic</b>. So we are in for another addition to our derived class <tt class="LITERAL">PizzaEntryImpl</tt>. So far, the constructor has been empty; now we will fill it with content.</p>
			<p>The task at hand is not very difficult, but we need to distinguish two different cases here: Checking the various toppings and selecting the <span class="GUILABEL">Family</span> size is static; we always want to do that. But checking the <span class="GUILABEL">Extra Cheese</span> check box on Mondays is not static; we need to compute the day of the week first. But for now, let's start with the simple case, the static selections.</p>
			<p>Whether a check box is checked or not is a property of the check box, and the same goes for radio buttons. So we can just use the property editor for setting these properties: Click on each of the toppings check boxes one by one and set the value of the property <tt class="LITERAL">checked</tt> to <tt class="LITERAL">true</tt> by selecting <tt class="LITERAL">true</tt> from the combo box in the value column. Do the same for the <span class="GUILABEL">Family</span> radio button. That's it.</p>
			<p>Now for the <span class="GUILABEL">Extra Cheese</span> check box. Since we said that we do not know before runtime whether the check box should be checked or not when the pizza entry form is shown, we cannot use the property editor but have to do this programmatically. In theory, all we need to do is determine whether it is a Monday, call <tt class="LITERAL">setChecked()</tt> on the <span class="GUILABEL">Extra Cheese</span> check box, and pass the desired value. The question is, how do we get access to this widget?</p>
			<p>Of course, situations like this are very common, which is why Qt Designer provides a very simple solution: All the widgets are represented as pointers with <tt class="LITERAL">public</tt> access in the generated base class. All you need to do is find out the correct widget pointer and call <tt class="LITERAL">setChecked()</tt> on it.</p>
			<p>If you check the generated file <tt class="FILENAME">PizzaEntry.h</tt>, you will see that the widget pointers have non-obvious names like <tt class="LITERAL">CheckBox1</tt> or <tt class="LITERAL">RadioButton3</tt>. You might wish that you had assigned the <tt class="LITERAL">name</tt> properties of all the widgets sensible names, and in fact it is not too late to do that: You could just load the file <tt class="FILENAME">pizza.ui</tt> into Qt Designer, change all <tt class="LITERAL">name</tt> properties, save the file, and regenerate <tt class="FILENAME">PizzaEntry.h</tt> and <tt class="FILENAME">PizzaEntry.cpp</tt>.</p>
			<p>On the other hand, this is just a simple example, so we might get away with just guessing or checking in Qt Designer which variable represents the <span class="GUILABEL">Extra Cheese</span> check box. Here is what we guessed&#151;the name might be slightly different for you, depending on the order in which you originally inserted the widgets:</p>
			<pre class="SCREEN">#include &lt;qcheckbox.h&gt;
#include &lt;qradiobutton.h&gt;
#include &lt;qdatetime.h&gt;

PizzaEntryImpl::PizzaEntryImpl( QWidget* parent,
                                const char* name, bool modal, WFlags f ) :
    PizzaEntry( parent, name, modal, f )
{
  QDate date = QDate::currentDate();
  if( date.dayOfWeek() == 1 )
    CheckBox5-&gt;setChecked( true );
  else
    CheckBox5-&gt;setChecked( false );
}</pre>
			<p>As you can see, we use the class <tt class="LITERAL">QDate</tt> and its methods <tt class="LITERAL">currentDate()</tt> and <tt class="LITERAL">dayOfWeek()</tt> to find out whether the current day is a Monday. Based on this information, we either call <tt class="LITERAL">setChecked( true )</tt> or <tt class="LITERAL">setChecked( false )</tt>.</p>
			<p>Add this code to your file <tt class="FILENAME">PizzaEntryImpl.cpp</tt> and don't forget to remove the empty constructor implementation from <tt class="FILENAME">PizzaEntryImpl.h</tt>. Compile your program with exactly the same commands as in the previous section and run it. The widgets should now be initialized as specified.</p>
		</div>
		<div class="NAVFOOTER">
			<br>
			<br>
			<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="black">
				<tr>
					<td width="33%" align="left" valign="top"><a href="chap3_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="chap3_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">Adding Functionality To The Push Buttons</font></td>
					<td width="34%" align="center" valign="top"><a href="chap3_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">Connecting Widgets To Each Other</font></td>
				</tr>
			</table>
		</div>
	</body>

</html>