You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/statistics-example.html

304 lines
12 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Table Example</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&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;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 Example</h1>
<p>
Another <a href="tqtable.html">TQTable</a> example.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/statistics.h 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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef STATISTICS_H
#define STATISTICS_H
#include &lt;<a href="tqtable-h.html">tqtable.h</a>&gt;
#include &lt;<a href="tqcombobox-h.html">tqcombobox.h</a>&gt;
class TableItem : public <a href="tqtableitem.html">TQTableItem</a>
{
public:
TableItem( <a href="tqtable.html">TQTable</a> *t, EditType et, const <a href="tqstring.html">TQString</a> &amp;txt ) : <a href="tqtableitem.html">TQTableItem</a>( t, et, txt ) {}
void paint( <a href="tqpainter.html">TQPainter</a> *p, const <a href="tqcolorgroup.html">TQColorGroup</a> &amp;cg, const <a href="tqrect.html">TQRect</a> &amp;cr, bool selected );
};
class ComboItem : public <a href="tqtableitem.html">TQTableItem</a>
{
public:
ComboItem( <a href="tqtable.html">TQTable</a> *t, EditType et );
<a href="tqwidget.html">TQWidget</a> *createEditor() const;
void setContentFromEditor( <a href="tqwidget.html">TQWidget</a> *w );
void setText( const <a href="tqstring.html">TQString</a> &amp;s );
private:
<a href="tqcombobox.html">TQComboBox</a> *cb;
};
class Table : public <a href="tqtable.html">TQTable</a>
{
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
Table();
void sortColumn( int col, bool ascending, bool wholeRows );
private slots:
void recalcSum( int row, int col );
private:
void initTable();
};
#endif
</pre>
<p> <hr>
<p> Implementation:
<p> <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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "statistics.h"
#include &lt;<a href="tqdir-h.html">tqdir.h</a>&gt;
#include &lt;<a href="tqstringlist-h.html">tqstringlist.h</a>&gt;
#include &lt;<a href="tqheader-h.html">tqheader.h</a>&gt;
#include &lt;<a href="tqcombobox-h.html">tqcombobox.h</a>&gt;
#include &lt;stdlib.h&gt;
const char* dirs[] = {
"kernel",
"tools",
"widgets",
"dialogs",
"xml",
"table",
"network",
"opengl",
"canvas",
0
};
<a name="f578"></a>Table::Table()
: <a href="tqtable.html">TQTable</a>( 10, 100, 0, "table" )
{
<a href="tqtable.html#setSorting">setSorting</a>( TRUE );
<a href="tqtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 0, tr( "File" ) );
<a href="tqtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 1, tr( "Size (bytes)" ) );
<a href="tqtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 2, tr( "Use in Sum" ) );
initTable();
<a href="tqtable.html#adjustColumn">adjustColumn</a>( 0 );
// if the user edited something we might need to recalculate the sum
<a href="tqobject.html#connect">connect</a>( this, TQ_SIGNAL( <a href="tqtable.html#valueChanged">valueChanged</a>( int, int ) ),
this, TQ_SLOT( recalcSum( int, int ) ) );
}
void <a name="f579"></a>Table::initTable()
{
// read all the TQt source and header files into a list
<a href="tqstringlist.html">TQStringList</a> all;
int i = 0;
<a href="tqstring.html">TQString</a> srcdir( "../../../src/" );
while ( dirs[ i ] ) {
<a href="tqdir.html">TQDir</a> dir( srcdir + dirs[ i ] );
<a name="x2780"></a> <a href="tqstringlist.html">TQStringList</a> lst = dir.<a href="tqdir.html#entryList">entryList</a>( "*.cpp; *.h" );
<a name="x2791"></a><a name="x2789"></a> for ( TQStringList::Iterator it = lst.<a href="tqvaluelist.html#begin">begin</a>(); it != lst.<a href="tqvaluelist.html#end">end</a>(); ++it ) {
if ( ( *it ).contains( "moc" ) )
continue;
all &lt;&lt; (TQString( dirs[ i ] ) + "/" + *it);
}
++i;
}
// set the number of rows we'll need for the table
<a name="x2790"></a> <a href="tqtable.html#setNumRows">setNumRows</a>( all.<a href="tqvaluelist.html#count">count</a>() + 1 );
i = 0;
int sum = 0;
// insert the data into the table
for ( TQStringList::Iterator it = all.<a href="tqvaluelist.html#begin">begin</a>(); it != all.<a href="tqvaluelist.html#end">end</a>(); ++it ) {
<a href="tqtable.html#setText">setText</a>( i, 0, *it );
<a href="tqfile.html">TQFile</a> f( srcdir + *it );
<a name="x2781"></a> <a href="tqtable.html#setText">setText</a>( i, 1, TQString::number( (ulong)f.<a href="tqfile.html#size">size</a>() ) );
ComboItem *ci = new ComboItem( this, TQTableItem::WhenCurrent );
<a href="tqtable.html#setItem">setItem</a>( i++, 2, ci );
sum += f.<a href="tqfile.html#size">size</a>();
}
// last row should show the sum
TableItem *i1 = new TableItem( this, TQTableItem::Never, tr( "Sum" ) );
<a href="tqtable.html#setItem">setItem</a>( i, 0, i1 );
TableItem *i2 = new TableItem( this, TQTableItem::Never, TQString::number( sum ) );
<a href="tqtable.html#setItem">setItem</a>( i, 1, i2 );
}
void <a name="f580"></a>Table::recalcSum( int, int col )
{
// only recalc if a value in the second or third column changed
if ( col &lt; 1 || col &gt; 2 )
return;
// recalc sum
int sum = 0;
for ( int i = 0; i &lt; numRows() - 1; ++i ) {
if ( <a href="tqtableitem.html#text">text</a>( i, 2 ) == "No" )
continue;
sum += <a href="tqtableitem.html#text">text</a>( i, 1 ).toInt();
}
// insert calculated data
TableItem *i1 = new TableItem( this, TQTableItem::Never, tr( "Sum" ) );
setItem( numRows() - 1, 0, i1 );
TableItem *i2 = new TableItem( this, TQTableItem::Never, TQString::number( sum ) );
setItem( numRows() - 1, 1, i2 );
}
<a name="x2784"></a>void Table::<a href="tqtable.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
TQTable::<a href="tqtable.html#sortColumn">sortColumn</a>( col, ascending, TRUE );
// re-insert sum row
recalcSum( 0, 1 );
}
<a name="x2786"></a>void TableItem::<a href="tqtableitem.html#paint">paint</a>( <a href="tqpainter.html">TQPainter</a> *p, const <a href="tqcolorgroup.html">TQColorGroup</a> &amp;cg, const <a href="tqrect.html">TQRect</a> &amp;cr, bool selected )
{
<a href="tqcolorgroup.html">TQColorGroup</a> g( cg );
// last row is the sum row - we want to make it more visible by
// using a red background
if ( <a href="tqtableitem.html#row">row</a>() == <a href="tqtableitem.html#table">table</a>()-&gt;numRows() - 1 )
<a name="x2779"></a> g.<a href="tqcolorgroup.html#setColor">setColor</a>( TQColorGroup::Base, red );
TQTableItem::<a href="tqtableitem.html#paint">paint</a>( p, g, cr, selected );
}
<a name="f577"></a>ComboItem::ComboItem( <a href="tqtable.html">TQTable</a> *t, EditType et )
: <a href="tqtableitem.html">TQTableItem</a>( t, et, "Yes" ), cb( 0 )
{
// we do not want this item to be replaced
<a href="tqtableitem.html#setReplaceable">setReplaceable</a>( FALSE );
}
<a name="x2785"></a>TQWidget *ComboItem::<a href="tqtableitem.html#createEditor">createEditor</a>() const
{
// create an editor - a combobox in our case
( (ComboItem*)this )-&gt;cb = new <a href="tqcombobox.html">TQComboBox</a>( <a href="tqtableitem.html#table">table</a>()-&gt;viewport() );
TQObject::<a href="tqobject.html#connect">connect</a>( cb, TQ_SIGNAL( activated( int ) ), table(), TQ_SLOT( doValueChanged() ) );
cb-&gt;insertItem( "Yes" );
cb-&gt;insertItem( "No" );
// and initialize it
cb-&gt;setCurrentItem( <a href="tqtableitem.html#text">text</a>() == "No" ? 1 : 0 );
return cb;
}
<a name="x2787"></a>void ComboItem::<a href="tqtableitem.html#setContentFromEditor">setContentFromEditor</a>( <a href="tqwidget.html">TQWidget</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-&gt;<a href="tqobject.html#inherits">inherits</a>( "TQComboBox" ) )
<a href="tqtableitem.html#setText">setText</a>( ( (TQComboBox*)w )-&gt;currentText() );
else
TQTableItem::<a href="tqtableitem.html#setContentFromEditor">setContentFromEditor</a>( w );
}
<a name="x2788"></a>void ComboItem::<a href="tqtableitem.html#setText">setText</a>( const <a href="tqstring.html">TQString</a> &amp;s )
{
if ( cb ) {
// initialize the combobox from the text
if ( s == "No" )
cb-&gt;setCurrentItem( 1 );
else
cb-&gt;setCurrentItem( 0 );
}
TQTableItem::<a href="tqtableitem.html#setText">setText</a>( s );
}
</pre>
<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "statistics.h"
#include &lt;<a href="tqapplication-h.html">tqapplication.h</a>&gt;
int main( int argc, char **argv )
{
<a href="tqapplication.html">TQApplication</a> a(argc,argv);
Table t;
a.<a href="tqapplication.html#setMainWidget">setMainWidget</a>( &amp;t );
t.<a href="tqwidget.html#show">show</a>();
return a.<a href="tqapplication.html#exec">exec</a>();
}
</pre>
<p>See also <a href="table-examples.html">Table Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>