[190] | 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/examples/table/statistics/statistics.doc:1 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Table Example</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { 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 Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main 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 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 Example</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | Another <a href="qtable.html">QTable</a> example.
|
---|
| 37 | <p> <hr>
|
---|
| 38 | <p> Header file:
|
---|
| 39 | <p> <pre>/****************************************************************************
|
---|
| 40 | ** $Id: statistics-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 41 | **
|
---|
| 42 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 43 | **
|
---|
| 44 | ** This file is part of an example program for Qt. This example
|
---|
| 45 | ** program may be used, distributed and modified without limitation.
|
---|
| 46 | **
|
---|
| 47 | *****************************************************************************/
|
---|
| 48 |
|
---|
| 49 | #ifndef STATISTICS_H
|
---|
| 50 | #define STATISTICS_H
|
---|
| 51 |
|
---|
| 52 | #include <<a href="qtable-h.html">qtable.h</a>>
|
---|
| 53 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
| 54 |
|
---|
| 55 | class TableItem : public <a href="qtableitem.html">QTableItem</a>
|
---|
| 56 | {
|
---|
| 57 | public:
|
---|
| 58 | TableItem( <a href="qtable.html">QTable</a> *t, EditType et, const <a href="qstring.html">QString</a> &txt ) : <a href="qtableitem.html">QTableItem</a>( t, et, txt ) {}
|
---|
| 59 | void paint( <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 );
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | class ComboItem : public <a href="qtableitem.html">QTableItem</a>
|
---|
| 63 | {
|
---|
| 64 | public:
|
---|
| 65 | ComboItem( <a href="qtable.html">QTable</a> *t, EditType et );
|
---|
| 66 | <a href="qwidget.html">QWidget</a> *createEditor() const;
|
---|
| 67 | void setContentFromEditor( <a href="qwidget.html">QWidget</a> *w );
|
---|
| 68 | void setText( const <a href="qstring.html">QString</a> &s );
|
---|
| 69 |
|
---|
| 70 | private:
|
---|
| 71 | <a href="qcombobox.html">QComboBox</a> *cb;
|
---|
| 72 |
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | class Table : public <a href="qtable.html">QTable</a>
|
---|
| 76 | {
|
---|
| 77 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
| 78 |
|
---|
| 79 | public:
|
---|
| 80 | Table();
|
---|
| 81 | void sortColumn( int col, bool ascending, bool wholeRows );
|
---|
| 82 |
|
---|
| 83 | private slots:
|
---|
| 84 | void recalcSum( int row, int col );
|
---|
| 85 |
|
---|
| 86 | private:
|
---|
| 87 | void initTable();
|
---|
| 88 |
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | #endif
|
---|
| 92 | </pre>
|
---|
| 93 |
|
---|
| 94 | <p> <hr>
|
---|
| 95 | <p> Implementation:
|
---|
| 96 | <p> <pre>/****************************************************************************
|
---|
| 97 | ** $Id: statistics-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 98 | **
|
---|
| 99 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 100 | **
|
---|
| 101 | ** This file is part of an example program for Qt. This example
|
---|
| 102 | ** program may be used, distributed and modified without limitation.
|
---|
| 103 | **
|
---|
| 104 | *****************************************************************************/
|
---|
| 105 |
|
---|
| 106 | #include "statistics.h"
|
---|
| 107 |
|
---|
| 108 | #include <<a href="qdir-h.html">qdir.h</a>>
|
---|
| 109 | #include <<a href="qstringlist-h.html">qstringlist.h</a>>
|
---|
| 110 | #include <<a href="qheader-h.html">qheader.h</a>>
|
---|
| 111 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
| 112 | #include <stdlib.h>
|
---|
| 113 |
|
---|
| 114 | const char* dirs[] = {
|
---|
| 115 | "kernel",
|
---|
| 116 | "tools",
|
---|
| 117 | "widgets",
|
---|
| 118 | "dialogs",
|
---|
| 119 | "xml",
|
---|
| 120 | "table",
|
---|
| 121 | "network",
|
---|
| 122 | "opengl",
|
---|
| 123 | "canvas",
|
---|
| 124 | 0
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | <a name="f578"></a>Table::Table()
|
---|
| 128 | : <a href="qtable.html">QTable</a>( 10, 100, 0, "table" )
|
---|
| 129 | {
|
---|
| 130 | <a href="qtable.html#setSorting">setSorting</a>( TRUE );
|
---|
| 131 | <a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 0, tr( "File" ) );
|
---|
| 132 | <a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 1, tr( "Size (bytes)" ) );
|
---|
| 133 | <a href="qtable.html#horizontalHeader">horizontalHeader</a>()->setLabel( 2, tr( "Use in Sum" ) );
|
---|
| 134 | initTable();
|
---|
| 135 | <a href="qtable.html#adjustColumn">adjustColumn</a>( 0 );
|
---|
| 136 |
|
---|
| 137 | // if the user edited something we might need to recalculate the sum
|
---|
| 138 | <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qtable.html#valueChanged">valueChanged</a>( int, int ) ),
|
---|
| 139 | this, SLOT( recalcSum( int, int ) ) );
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void <a name="f579"></a>Table::initTable()
|
---|
| 143 | {
|
---|
| 144 | // read all the Qt source and header files into a list
|
---|
| 145 | <a href="qstringlist.html">QStringList</a> all;
|
---|
| 146 | int i = 0;
|
---|
| 147 | <a href="qstring.html">QString</a> srcdir( "../../../src/" );
|
---|
| 148 | while ( dirs[ i ] ) {
|
---|
| 149 | <a href="qdir.html">QDir</a> dir( srcdir + dirs[ i ] );
|
---|
| 150 | <a name="x2780"></a> <a href="qstringlist.html">QStringList</a> lst = dir.<a href="qdir.html#entryList">entryList</a>( "*.cpp; *.h" );
|
---|
| 151 | <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 ) {
|
---|
| 152 | if ( ( *it ).contains( "moc" ) )
|
---|
| 153 | continue;
|
---|
| 154 | all << (QString( dirs[ i ] ) + "/" + *it);
|
---|
| 155 | }
|
---|
| 156 | ++i;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // set the number of rows we'll need for the table
|
---|
| 160 | <a name="x2790"></a> <a href="qtable.html#setNumRows">setNumRows</a>( all.<a href="qvaluelist.html#count">count</a>() + 1 );
|
---|
| 161 | i = 0;
|
---|
| 162 | int sum = 0;
|
---|
| 163 |
|
---|
| 164 | // insert the data into the table
|
---|
| 165 | for ( QStringList::Iterator it = all.<a href="qvaluelist.html#begin">begin</a>(); it != all.<a href="qvaluelist.html#end">end</a>(); ++it ) {
|
---|
| 166 | <a href="qtable.html#setText">setText</a>( i, 0, *it );
|
---|
| 167 | <a href="qfile.html">QFile</a> f( srcdir + *it );
|
---|
| 168 | <a name="x2781"></a> <a href="qtable.html#setText">setText</a>( i, 1, QString::number( (ulong)f.<a href="qfile.html#size">size</a>() ) );
|
---|
| 169 | ComboItem *ci = new ComboItem( this, QTableItem::WhenCurrent );
|
---|
| 170 | <a href="qtable.html#setItem">setItem</a>( i++, 2, ci );
|
---|
| 171 | sum += f.<a href="qfile.html#size">size</a>();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | // last row should show the sum
|
---|
| 175 | TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
|
---|
| 176 | <a href="qtable.html#setItem">setItem</a>( i, 0, i1 );
|
---|
| 177 | TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
|
---|
| 178 | <a href="qtable.html#setItem">setItem</a>( i, 1, i2 );
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void <a name="f580"></a>Table::recalcSum( int, int col )
|
---|
| 182 | {
|
---|
| 183 | // only recalc if a value in the second or third column changed
|
---|
| 184 | if ( col < 1 || col > 2 )
|
---|
| 185 | return;
|
---|
| 186 |
|
---|
| 187 | // recalc sum
|
---|
| 188 | int sum = 0;
|
---|
| 189 | for ( int i = 0; i < numRows() - 1; ++i ) {
|
---|
| 190 | if ( <a href="qtableitem.html#text">text</a>( i, 2 ) == "No" )
|
---|
| 191 | continue;
|
---|
| 192 | sum += <a href="qtableitem.html#text">text</a>( i, 1 ).toInt();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | // insert calculated data
|
---|
| 196 | TableItem *i1 = new TableItem( this, QTableItem::Never, tr( "Sum" ) );
|
---|
| 197 | setItem( numRows() - 1, 0, i1 );
|
---|
| 198 | TableItem *i2 = new TableItem( this, QTableItem::Never, QString::number( sum ) );
|
---|
| 199 | setItem( numRows() - 1, 1, i2 );
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | <a name="x2784"></a>void Table::<a href="qtable.html#sortColumn">sortColumn</a>( int col, bool ascending, bool /*wholeRows*/ )
|
---|
| 203 | {
|
---|
| 204 | // sum row should not be sorted, so get rid of it for now
|
---|
| 205 | clearCell( numRows() - 1, 0 );
|
---|
| 206 | clearCell( numRows() - 1, 1 );
|
---|
| 207 | // do sort
|
---|
| 208 | QTable::<a href="qtable.html#sortColumn">sortColumn</a>( col, ascending, TRUE );
|
---|
| 209 | // re-insert sum row
|
---|
| 210 | recalcSum( 0, 1 );
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | <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 )
|
---|
| 216 | {
|
---|
| 217 | <a href="qcolorgroup.html">QColorGroup</a> g( cg );
|
---|
| 218 | // last row is the sum row - we want to make it more visible by
|
---|
| 219 | // using a red background
|
---|
| 220 | if ( <a href="qtableitem.html#row">row</a>() == <a href="qtableitem.html#table">table</a>()->numRows() - 1 )
|
---|
| 221 | <a name="x2779"></a> g.<a href="qcolorgroup.html#setColor">setColor</a>( QColorGroup::Base, red );
|
---|
| 222 | QTableItem::<a href="qtableitem.html#paint">paint</a>( p, g, cr, selected );
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | <a name="f577"></a>ComboItem::ComboItem( <a href="qtable.html">QTable</a> *t, EditType et )
|
---|
| 229 | : <a href="qtableitem.html">QTableItem</a>( t, et, "Yes" ), cb( 0 )
|
---|
| 230 | {
|
---|
| 231 | // we do not want this item to be replaced
|
---|
| 232 | <a href="qtableitem.html#setReplaceable">setReplaceable</a>( FALSE );
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | <a name="x2785"></a>QWidget *ComboItem::<a href="qtableitem.html#createEditor">createEditor</a>() const
|
---|
| 236 | {
|
---|
| 237 | // create an editor - a combobox in our case
|
---|
| 238 | ( (ComboItem*)this )->cb = new <a href="qcombobox.html">QComboBox</a>( <a href="qtableitem.html#table">table</a>()->viewport() );
|
---|
| 239 | QObject::<a href="qobject.html#connect">connect</a>( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) );
|
---|
| 240 | cb->insertItem( "Yes" );
|
---|
| 241 | cb->insertItem( "No" );
|
---|
| 242 | // and initialize it
|
---|
| 243 | cb->setCurrentItem( <a href="qtableitem.html#text">text</a>() == "No" ? 1 : 0 );
|
---|
| 244 | return cb;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | <a name="x2787"></a>void ComboItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( <a href="qwidget.html">QWidget</a> *w )
|
---|
| 248 | {
|
---|
| 249 | // the user changed the value of the combobox, so synchronize the
|
---|
| 250 | // value of the item (its text), with the value of the combobox
|
---|
| 251 | <a name="x2783"></a> if ( w-><a href="qobject.html#inherits">inherits</a>( "QComboBox" ) )
|
---|
| 252 | <a href="qtableitem.html#setText">setText</a>( ( (QComboBox*)w )->currentText() );
|
---|
| 253 | else
|
---|
| 254 | QTableItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( w );
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | <a name="x2788"></a>void ComboItem::<a href="qtableitem.html#setText">setText</a>( const <a href="qstring.html">QString</a> &s )
|
---|
| 258 | {
|
---|
| 259 | if ( cb ) {
|
---|
| 260 | // initialize the combobox from the text
|
---|
| 261 | if ( s == "No" )
|
---|
| 262 | cb->setCurrentItem( 1 );
|
---|
| 263 | else
|
---|
| 264 | cb->setCurrentItem( 0 );
|
---|
| 265 | }
|
---|
| 266 | QTableItem::<a href="qtableitem.html#setText">setText</a>( s );
|
---|
| 267 | }
|
---|
| 268 | </pre>
|
---|
| 269 |
|
---|
| 270 | <p> <hr>
|
---|
| 271 | <p> Main:
|
---|
| 272 | <p> <pre>/****************************************************************************
|
---|
| 273 | ** $Id: statistics-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 274 | **
|
---|
| 275 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 276 | **
|
---|
| 277 | ** This file is part of an example program for Qt. This example
|
---|
| 278 | ** program may be used, distributed and modified without limitation.
|
---|
| 279 | **
|
---|
| 280 | *****************************************************************************/
|
---|
| 281 |
|
---|
| 282 | #include "statistics.h"
|
---|
| 283 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 284 | int main( int argc, char **argv )
|
---|
| 285 | {
|
---|
| 286 | <a href="qapplication.html">QApplication</a> a(argc,argv);
|
---|
| 287 |
|
---|
| 288 | Table t;
|
---|
| 289 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &t );
|
---|
| 290 | t.<a href="qwidget.html#show">show</a>();
|
---|
| 291 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 292 | }
|
---|
| 293 | </pre>
|
---|
| 294 |
|
---|
| 295 | <p>See also <a href="table-examples.html">Table Examples</a>.
|
---|
| 296 |
|
---|
| 297 | <!-- eof -->
|
---|
| 298 | <p><address><hr><div align=center>
|
---|
| 299 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 300 | <td>Copyright © 2007
|
---|
| 301 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 302 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 303 | </table></div></address></body>
|
---|
| 304 | </html>
|
---|