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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/table/qtable.cpp:1657 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>table/statistics/statistics.cpp Example File</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>
<pre>/****************************************************************************
** $Id: qt/statistics.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "statistics.h"
#include <<a href="qdir-h.html">qdir.h</a>>
#include <<a href="qstringlist-h.html">qstringlist.h</a>>
#include <<a href="qheader-h.html">qheader.h</a>>
#include <<a href="qcombobox-h.html">qcombobox.h</a>>
#include <stdlib.h>
const char* dirs[] = {
"kernel",
"tools",
"widgets",
"dialogs",
"xml",
"table",
"network",
"opengl",
"canvas",
0
};
<a name="f145"></a>Table::Table()
: <a href="qtable.html">QTable</a>( 10, 100, 0, "table" )
{
<a href="qtable.html#setSorting">setSorting</a>( TRUE );
<a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 0, tr( "File" ) );
<a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 1, tr( "Size (bytes)" ) );
<a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 2, tr( "Use in Sum" ) );
initTable();
<a href="qtable.html#adjustColumn">adjustColumn</a>( 0 );
// if the user edited something we might need to recalculate the sum
<a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qtable.html#valueChanged">valueChanged</a>( int, int ) ),
this, SLOT( recalcSum( int, int ) ) );
}
void <a name="f146"></a>Table::initTable()
{
// read all the Qt source and header files into a list
<a href="qstringlist.html">QStringList</a> all;
int i = 0;
<a href="qstring.html">QString</a> srcdir( "../../../src/" );
while ( dirs[ i ] ) {
<a href="qdir.html">QDir</a> dir( srcdir + dirs[ i ] );
<a name="x2780"></a> <a href="qstringlist.html">QStringList</a> lst = dir.<a href="qdir.html#entryList">entryList</a>( "*.cpp; *.h" );
<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 ) {
if ( ( *it ).contains( "moc" ) )
continue;
all << (QString( dirs[ i ] ) + "/" + *it);
}
++i;
}
// set the number of rows we'll need for the table
<a name="x2790"></a> <a href="qtable.html#setNumRows">setNumRows</a>( all.<a href="qvaluelist.html#count">count</a>() + 1 );
i = 0;
int sum = 0;
// insert the data into the table
for ( QStringList::Iterator it = all.<a href="qvaluelist.html#begin">begin</a>(); it != all.<a href="qvaluelist.html#end">end</a>(); ++it ) {
<a href="qtable.html#setText">setText</a>( i, 0, *it );
<a href="qfile.html">QFile</a> f( srcdir + *it );
<a name="x2781"></a> <a href="qtable.html#setText">setText</a>( i, 1, QString::number( (ulong)f.<a href="qfile.html#size">size</a>() ) );
ComboItem *ci = new ComboItem( this, QTableItem::WhenCurrent );
<a href="qtable.html#setItem">setItem</a>( i++, 2, ci );
sum += f.<a href="qfile.html#size">size</a>();
}
// last row should show the sum
TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
<a href="qtable.html#setItem">setItem</a>( i, 0, i1 );
TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
<a href="qtable.html#setItem">setItem</a>( i, 1, i2 );
}
void <a name="f147"></a>Table::recalcSum( int, int col )
{
// only recalc if a value in the second or third column changed
if ( col < 1 || col > 2 )
return;
// recalc sum
int sum = 0;
for ( int i = 0; i < numRows() - 1; ++i ) {
if ( <a href="qtableitem.html#text">text</a>( i, 2 ) == "No" )
continue;
sum += <a href="qtableitem.html#text">text</a>( i, 1 ).toInt();
}
// insert calculated data
TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
setItem( numRows() - 1, 0, i1 );
TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
setItem( numRows() - 1, 1, i2 );
}
<a name="x2784"></a>void Table::<a href="qtable.html#sortColumn">sortColumn</a>( int col, bool ascending, bool /*wholeRows*/ )
{
// sum row should not be sorted, so get rid of it for now
clearCell( numRows() - 1, 0 );
clearCell( numRows() - 1, 1 );
// do sort
QTable::<a href="qtable.html#sortColumn">sortColumn</a>( col, ascending, TRUE );
// re-insert sum row
recalcSum( 0, 1 );
}
<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> &cg, const <a href="qrect.html">QRect</a> &cr, bool selected )
{
<a href="qcolorgroup.html">QColorGroup</a> g( cg );
// last row is the sum row - we want to make it more visible by
// using a red background
if ( <a href="qtableitem.html#row">row</a>() == <a href="qtableitem.html#table">table</a>()->numRows() - 1 )
<a name="x2779"></a> g.<a href="qcolorgroup.html#setColor">setColor</a>( QColorGroup::Base, red );
QTableItem::<a href="qtableitem.html#paint">paint</a>( p, g, cr, selected );
}
<a name="f144"></a>ComboItem::ComboItem( <a href="qtable.html">QTable</a> *t, EditType et )
: <a href="qtableitem.html">QTableItem</a>( t, et, "Yes" ), cb( 0 )
{
// we do not want this item to be replaced
<a href="qtableitem.html#setReplaceable">setReplaceable</a>( FALSE );
}
<a name="x2785"></a>QWidget *ComboItem::<a href="qtableitem.html#createEditor">createEditor</a>() const
{
// create an editor - a combobox in our case
( (ComboItem*)this )->cb = new <a href="qcombobox.html">QComboBox</a>( <a href="qtableitem.html#table">table</a>()->viewport() );
QObject::<a href="qobject.html#connect">connect</a>( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) );
cb->insertItem( "Yes" );
cb->insertItem( "No" );
// and initialize it
cb->setCurrentItem( <a href="qtableitem.html#text">text</a>() == "No" ? 1 : 0 );
return cb;
}
<a name="x2787"></a>void ComboItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( <a href="qwidget.html">QWidget</a> *w )
{
// the user changed the value of the combobox, so synchronize the
// value of the item (its text), with the value of the combobox
<a name="x2783"></a> if ( w-><a href="qobject.html#inherits">inherits</a>( "QComboBox" ) )
<a href="qtableitem.html#setText">setText</a>( ( (QComboBox*)w )->currentText() );
else
QTableItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( w );
}
<a name="x2788"></a>void ComboItem::<a href="qtableitem.html#setText">setText</a>( const <a href="qstring.html">QString</a> &s )
{
if ( cb ) {
// initialize the combobox from the text
if ( s == "No" )
cb->setCurrentItem( 1 );
else
cb->setCurrentItem( 0 );
}
QTableItem::<a href="qtableitem.html#setText">setText</a>( s );
}
</pre><!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>
|