source: trunk/doc/html/table-statistics-statistics-cpp.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 9.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/table/qtable.cpp:1657 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>table/statistics/statistics.cpp Example File</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>table/statistics/statistics.cpp Example File</h1>
33
34
35<pre>/****************************************************************************
36** $Id: table-statistics-statistics-cpp.html 2051 2007-02-21 10:04:20Z chehrlic $
37**
38** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
39**
40** This file is part of an example program for Qt. This example
41** program may be used, distributed and modified without limitation.
42**
43*****************************************************************************/
44
45#include "statistics.h"
46
47#include &lt;<a href="qdir-h.html">qdir.h</a>&gt;
48#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;
49#include &lt;<a href="qheader-h.html">qheader.h</a>&gt;
50#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
51#include &lt;stdlib.h&gt;
52
53const char* dirs[] = {
54 "kernel",
55 "tools",
56 "widgets",
57 "dialogs",
58 "xml",
59 "table",
60 "network",
61 "opengl",
62 "canvas",
63 0
64};
65
66<a name="f145"></a>Table::Table()
67 : <a href="qtable.html">QTable</a>( 10, 100, 0, "table" )
68{
69 <a href="qtable.html#setSorting">setSorting</a>( TRUE );
70 <a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 0, tr( "File" ) );
71 <a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 1, tr( "Size (bytes)" ) );
72 <a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 2, tr( "Use in Sum" ) );
73 initTable();
74 <a href="qtable.html#adjustColumn">adjustColumn</a>( 0 );
75
76 // if the user edited something we might need to recalculate the sum
77 <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qtable.html#valueChanged">valueChanged</a>( int, int ) ),
78 this, SLOT( recalcSum( int, int ) ) );
79}
80
81void <a name="f146"></a>Table::initTable()
82{
83 // read all the Qt source and header files into a list
84 <a href="qstringlist.html">QStringList</a> all;
85 int i = 0;
86 <a href="qstring.html">QString</a> srcdir( "../../../src/" );
87 while ( dirs[ i ] ) {
88 <a href="qdir.html">QDir</a> dir( srcdir + dirs[ i ] );
89<a name="x2780"></a> <a href="qstringlist.html">QStringList</a> lst = dir.<a href="qdir.html#entryList">entryList</a>( "*.cpp; *.h" );
90<a name="x2791"></a><a name="x2789"></a> for ( QStringList::Iterator it = lst.<a href="qvaluelist.html#begin">begin</a>(); it != lst.<a href="qvaluelist.html#end">end</a>(); ++it ) {
91 if ( ( *it ).contains( "moc" ) )
92 continue;
93 all &lt;&lt; (QString( dirs[ i ] ) + "/" + *it);
94 }
95 ++i;
96 }
97
98 // set the number of rows we'll need for the table
99<a name="x2790"></a> <a href="qtable.html#setNumRows">setNumRows</a>( all.<a href="qvaluelist.html#count">count</a>() + 1 );
100 i = 0;
101 int sum = 0;
102
103 // insert the data into the table
104 for ( QStringList::Iterator it = all.<a href="qvaluelist.html#begin">begin</a>(); it != all.<a href="qvaluelist.html#end">end</a>(); ++it ) {
105 <a href="qtable.html#setText">setText</a>( i, 0, *it );
106 <a href="qfile.html">QFile</a> f( srcdir + *it );
107<a name="x2781"></a> <a href="qtable.html#setText">setText</a>( i, 1, QString::number( (ulong)f.<a href="qfile.html#size">size</a>() ) );
108 ComboItem *ci = new ComboItem( this, QTableItem::WhenCurrent );
109 <a href="qtable.html#setItem">setItem</a>( i++, 2, ci );
110 sum += f.<a href="qfile.html#size">size</a>();
111 }
112
113 // last row should show the sum
114 TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
115 <a href="qtable.html#setItem">setItem</a>( i, 0, i1 );
116 TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
117 <a href="qtable.html#setItem">setItem</a>( i, 1, i2 );
118}
119
120void <a name="f147"></a>Table::recalcSum( int, int col )
121{
122 // only recalc if a value in the second or third column changed
123 if ( col &lt; 1 || col &gt; 2 )
124 return;
125
126 // recalc sum
127 int sum = 0;
128 for ( int i = 0; i &lt; numRows() - 1; ++i ) {
129 if ( <a href="qtableitem.html#text">text</a>( i, 2 ) == "No" )
130 continue;
131 sum += <a href="qtableitem.html#text">text</a>( i, 1 ).toInt();
132 }
133
134 // insert calculated data
135 TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
136 setItem( numRows() - 1, 0, i1 );
137 TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
138 setItem( numRows() - 1, 1, i2 );
139}
140
141<a name="x2784"></a>void Table::<a href="qtable.html#sortColumn">sortColumn</a>( int col, bool ascending, bool /*wholeRows*/ )
142{
143 // sum row should not be sorted, so get rid of it for now
144 clearCell( numRows() - 1, 0 );
145 clearCell( numRows() - 1, 1 );
146 // do sort
147 QTable::<a href="qtable.html#sortColumn">sortColumn</a>( col, ascending, TRUE );
148 // re-insert sum row
149 recalcSum( 0, 1 );
150}
151
152
153
154<a name="x2786"></a>void TableItem::<a href="qtableitem.html#paint">paint</a>( <a href="qpainter.html">QPainter</a> *p, const <a href="qcolorgroup.html">QColorGroup</a> &amp;cg, const <a href="qrect.html">QRect</a> &amp;cr, bool selected )
155{
156 <a href="qcolorgroup.html">QColorGroup</a> g( cg );
157 // last row is the sum row - we want to make it more visible by
158 // using a red background
159 if ( <a href="qtableitem.html#row">row</a>() == <a href="qtableitem.html#table">table</a>()-&gt;numRows() - 1 )
160<a name="x2779"></a> g.<a href="qcolorgroup.html#setColor">setColor</a>( QColorGroup::Base, red );
161 QTableItem::<a href="qtableitem.html#paint">paint</a>( p, g, cr, selected );
162}
163
164
165
166
167<a name="f144"></a>ComboItem::ComboItem( <a href="qtable.html">QTable</a> *t, EditType et )
168 : <a href="qtableitem.html">QTableItem</a>( t, et, "Yes" ), cb( 0 )
169{
170 // we do not want this item to be replaced
171 <a href="qtableitem.html#setReplaceable">setReplaceable</a>( FALSE );
172}
173
174<a name="x2785"></a>QWidget *ComboItem::<a href="qtableitem.html#createEditor">createEditor</a>() const
175{
176 // create an editor - a combobox in our case
177 ( (ComboItem*)this )-&gt;cb = new <a href="qcombobox.html">QComboBox</a>( <a href="qtableitem.html#table">table</a>()-&gt;viewport() );
178 QObject::<a href="qobject.html#connect">connect</a>( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) );
179 cb-&gt;insertItem( "Yes" );
180 cb-&gt;insertItem( "No" );
181 // and initialize it
182 cb-&gt;setCurrentItem( <a href="qtableitem.html#text">text</a>() == "No" ? 1 : 0 );
183 return cb;
184}
185
186<a name="x2787"></a>void ComboItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( <a href="qwidget.html">QWidget</a> *w )
187{
188 // the user changed the value of the combobox, so synchronize the
189 // value of the item (its text), with the value of the combobox
190<a name="x2783"></a> if ( w-&gt;<a href="qobject.html#inherits">inherits</a>( "QComboBox" ) )
191 <a href="qtableitem.html#setText">setText</a>( ( (QComboBox*)w )-&gt;currentText() );
192 else
193 QTableItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( w );
194}
195
196<a name="x2788"></a>void ComboItem::<a href="qtableitem.html#setText">setText</a>( const <a href="qstring.html">QString</a> &amp;s )
197{
198 if ( cb ) {
199 // initialize the combobox from the text
200 if ( s == "No" )
201 cb-&gt;setCurrentItem( 1 );
202 else
203 cb-&gt;setCurrentItem( 0 );
204 }
205 QTableItem::<a href="qtableitem.html#setText">setText</a>( s );
206}
207</pre><!-- eof -->
208<p><address><hr><div align=center>
209<table width=100% cellspacing=0 border=0><tr>
210<td>Copyright &copy; 2007
211<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
212<td align=right><div align=right>Qt 3.3.8</div>
213</table></div></address></body>
214</html>
Note: See TracBrowser for help on using the repository browser.