⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statistics-example.html

📁 QT3.0.5的帮助文档的中文翻译
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/table/statistics/statistics.doc:1 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="Translator" content="farfareast"><meta name="Qt zh_CN Documents Website" content="http://www.qiliang.net/qt"><title>表格样例</title><style type="text/css"><!--h3.fn,span.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; font-family: "Times New Roman" }--></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">主页</font></a> | <a href="classes.html"><font color="#004faf">所有的类</font></a> | <a href="mainclasses.html"><font color="#004faf">主要的类</font></a> | <a href="annotated.html"><font color="#004faf">注释的类</font></a> | <a href="groups.html"><font color="#004faf">分组的类</font></a> | <a href="functions.html"><font color="#004faf">函数</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>表格样例</h1>   <p>另一个<a href="qtable.html">QTable</a>样例。<p> <hr><p>头文件:<p> <pre></pre><p> <hr><p>实现:<p> <pre>/****************************************************************************** $Id:  qt/statistics.cpp   3.0.5   edited Feb 18 11:55 $**** Copyright (C) 1992-2000 Trolltech AS.  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 &lt;<a href="qdir-h.html">qdir.h</a>&gt;#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;#include &lt;<a href="qheader-h.html">qheader.h</a>&gt;#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;#include &lt;stdlib.h&gt;const char* dirs[] = {    "kernel",    "tools",    "widgets",    "dialogs",    "xml",    "table",    "network",    "opengl",    "canvas",    0};<a name="f575"></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>()-&gt;setLabel( 0, tr( "File" ) );    <a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;setLabel( 1, tr( "Size (bytes)" ) );    <a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;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="f576"></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> qtdir = getenv( "QTDIR" );    while ( dirs[ i ] ) {        <a href="qdir.html">QDir</a> dir( qtdir + "/src/" + dirs[ i ] );<a name="x2627"></a>        <a href="qstringlist.html">QStringList</a> lst = dir.<a href="qdir.html#entryList">entryList</a>( "*.cpp; *.h" );<a name="x2638"></a><a name="x2636"></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 &lt;&lt; QString( dirs[ i ] ) + "/" + *it;        }        ++i;    }    // set the number of rows we'll need for the table<a name="x2637"></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( qtdir + "/src/" + *it );<a name="x2628"></a>        <a href="qtable.html#setText">setText</a>( i, 1, QString::number( 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="f577"></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="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="x2631"></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="x2633"></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 ){    <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>()-&gt;numRows() - 1 )<a name="x2626"></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="f574"></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="x2632"></a>QWidget *ComboItem::<a href="qtableitem.html#createEditor">createEditor</a>() const{    // create an editor - a combobox in our case    ( (ComboItem*)this )-&gt;cb = new <a href="qcombobox.html">QComboBox</a>( <a href="qtableitem.html#table">table</a>()-&gt;viewport() );    QObject::<a href="qobject.html#connect">connect</a>( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) );    cb-&gt;insertItem( "Yes" );    cb-&gt;insertItem( "No" );    // and initialize it    cb-&gt;setCurrentItem( <a href="qtableitem.html#text">text</a>() == "No" ? 1 : 0 );    return cb;}<a name="x2634"></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="x2630"></a>    if ( w-&gt;<a href="qobject.html#inherits">inherits</a>( "QComboBox" ) )        <a href="qtableitem.html#setText">setText</a>( ( (QComboBox*)w )-&gt;currentText() );    else        QTableItem::<a href="qtableitem.html#setContentFromEditor">setContentFromEditor</a>( w );}<a name="x2635"></a>void ComboItem::<a href="qtableitem.html#setText">setText</a>( const <a href="qstring.html">QString</a> &amp;s ){    if ( cb ) {        // initialize the combobox from the text        if ( s == "No" )            cb-&gt;setCurrentItem( 1 );        else            cb-&gt;setCurrentItem( 0 );    }    QTableItem::<a href="qtableitem.html#setText">setText</a>( s );}</pre><p> <hr><p>主文件:<p> <pre>/****************************************************************************** $Id:  qt/main.cpp   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  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 &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;int main( int argc, char **argv ){    <a href="qapplication.html">QApplication</a> a(argc,argv);    Table t;    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;t );    t.<a href="qwidget.html#show">show</a>();    return a.<a href="qapplication.html#exec">exec</a>();}</pre><p>也参见<a href="table-examples.html">表格例程</a>。<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td><a href="zh_CN.html">译者:farfareast</a><td align=right><div align=right>Qt 3.0.5版</div></table></div></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -