Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 397cf13019f8c526877f271962f26f4c > files > 646

libqt3-devel-3.1.1-13mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.1-reggie-23625/qt-x11-free-3.1.1/examples/checklists/checklists.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Listviews with Checkable Items</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; }
--></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>Listviews with Checkable Items</h1>

 
<p> 
This example program shows how to use listviews with different types of
checkable items.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/checklists.h   3.1.1   edited Nov 8 10:35 $
**
** 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.
**
*****************************************************************************/

#ifndef CHECKLISTS_H
#define CHECKLISTS_H

#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;

class QListView;
class QLabel;

class CheckLists : public <a href="qwidget.html">QWidget</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    CheckLists( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );

protected:
    <a href="qlistview.html">QListView</a> *lv1, *lv2;
    <a href="qlabel.html">QLabel</a> *label;

protected slots:
    void copy1to2();
    void copy2to3();

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/checklists.cpp   3.1.1   edited Nov 8 10:35 $
**
** 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 "checklists.h"

#include &lt;<a href="qlistview-h.html">qlistview.h</a>&gt;
#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qvaluelist-h.html">qvaluelist.h</a>&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;

/*
 * Constructor
 *
 * Create all child widgets of the CheckList Widget
 */

<a name="f234"></a>CheckLists::CheckLists( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qwidget.html">QWidget</a>( parent, name )
{
    <a href="qhboxlayout.html">QHBoxLayout</a> *lay = new <a href="qhboxlayout.html">QHBoxLayout</a>( this );
<a name="x359"></a>    lay-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // create a widget which layouts its childs in a column
    <a href="qvboxlayout.html">QVBoxLayout</a> *vbox1 = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
    vbox1-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // First child: a Label
<a name="x356"></a>    vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Check some items!", this ) );

    // Second child: the ListView
    lv1 = new <a href="qlistview.html">QListView</a>( this );
    vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv1 );
<a name="x360"></a>    lv1-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "Items" );
<a name="x362"></a>    lv1-&gt;<a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );

    // create a list with 4 ListViewItems which will be parent items of other ListViewItems
    <a href="qvaluelist.html">QValueList</a>&lt;QListViewItem *&gt; parentList;

<a name="x367"></a>    parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qlistviewitem.html">QListViewItem</a>( lv1, "Parent Item 1" ) );
    parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qlistviewitem.html">QListViewItem</a>( lv1, "Parent Item 2" ) );
    parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qlistviewitem.html">QListViewItem</a>( lv1, "Parent Item 3" ) );
    parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qlistviewitem.html">QListViewItem</a>( lv1, "Parent Item 4" ) );

    <a href="qlistviewitem.html">QListViewItem</a> *item = 0;
    unsigned int num = 1;
    // go through the list of parent items...
<a name="x369"></a><a name="x368"></a>    for ( QValueList&lt;QListViewItem*&gt;::Iterator it = parentList.<a href="qvaluelist.html#begin">begin</a>(); it != parentList.<a href="qvaluelist.html#end">end</a>();
          ( *it )-&gt;setOpen( TRUE ), ++it, num++ ) {
        item = *it;
        // ...and create 5 checkable child ListViewItems for each parent item
        for ( unsigned int i = 1; i &lt;= 5; i++ )
            (void)new <a href="qchecklistitem.html">QCheckListItem</a>( item, QString( "%1. Child of Parent %2" ).arg( i ).arg( num ), QCheckListItem::CheckBox );
    }

    // Create another widget for layouting
    <a href="qvboxlayout.html">QVBoxLayout</a> *tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
    tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // create a pushbutton
    <a href="qpushbutton.html">QPushButton</a> *copy1 = new <a href="qpushbutton.html">QPushButton</a>( "  -&gt;  ", this );
    tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy1 );
<a name="x370"></a><a name="x366"></a>    copy1-&gt;<a href="qwidget.html#setMaximumWidth">setMaximumWidth</a>( copy1-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().width() );
    // connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
    <a href="qobject.html#connect">connect</a>( copy1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( copy1to2() ) );

    // another widget for layouting
    <a href="qvboxlayout.html">QVBoxLayout</a> *vbox2 = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
    vbox2-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // and another label
    vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Check one item!", this ) );

    // create the second listview
    lv2 = new <a href="qlistview.html">QListView</a>( this );
    vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv2 );
    lv2-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "Items" );
    lv2-&gt;<a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );

    // another widget needed for layouting only
    tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
    tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // create another pushbutton...
    <a href="qpushbutton.html">QPushButton</a> *copy2 = new <a href="qpushbutton.html">QPushButton</a>( "  -&gt;  ", this );
    lay-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy2 );
    copy2-&gt;<a href="qwidget.html#setMaximumWidth">setMaximumWidth</a>( copy2-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().width() );
    // ...and connect its clicked() SIGNAL to the copy2to3() SLOT
    <a href="qobject.html#connect">connect</a>( copy2, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( copy2to3() ) );

    tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
    tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );

    // and create a label which will be at the right of the window
    label = new <a href="qlabel.html">QLabel</a>( "No Item yet...", this );
    tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( label );
}

/*
 * SLOT copy1to2()
 *
 * Copies all checked ListViewItems from the first ListView to
 * the second one, and inserts them as Radio-ListViewItem.
 */

void <a name="f235"></a>CheckLists::copy1to2()
{
    // create an iterator which operates on the first ListView
    <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv1 );

<a name="x361"></a>    lv2-&gt;<a href="qlistview.html#clear">clear</a>();

    // Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
    // are inserted into a Listview, the parent item of these MUST be a controller Item!
    <a href="qchecklistitem.html">QCheckListItem</a> *item = new <a href="qchecklistitem.html">QCheckListItem</a>( lv2, "Controller", QCheckListItem::Controller );
<a name="x364"></a>    item-&gt;<a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );

    // iterate through the first ListView...
<a name="x365"></a>    for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
        // ...check state of childs, and...
        if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
            // ...if the item is checked...
            if ( ( (QCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
                // ...insert a Radio-ListViewItem with the same text into the second ListView
                (void)new <a href="qchecklistitem.html">QCheckListItem</a>( item, it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ), QCheckListItem::RadioButton );

<a name="x363"></a>    if ( item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )
        ( ( <a href="qchecklistitem.html">QCheckListItem</a>* )item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )-&gt;setOn( TRUE );
}

/*
 * SLOT copy2to3()
 *
 * Copies the checked item of the second ListView into the
 * Label at the right.
 */

void <a name="f236"></a>CheckLists::copy2to3()
{
    // create an iterator which operates on the second ListView
    <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv2 );

    label-&gt;<a href="qlabel.html#setText">setText</a>( "No Item checked" );

    // iterate through the second ListView...
    for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
        // ...check state of childs, and...
        if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
            // ...if the item is checked...
            if ( ( (QCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
                // ...set the text of the item to the label
                label-&gt;<a href="qlabel.html#setText">setText</a>( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ) );
}

</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.1.1   edited Nov 8 10:35 $
**
** 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 "checklists.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 );

    CheckLists checklists;
    checklists.<a href="qwidget.html#resize">resize</a>( 650, 350 );
<a name="x374"></a>    checklists.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - CheckLists" );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;checklists );
    checklists.<a href="qwidget.html#show">show</a>();

    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</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 align=right><div align=right>Qt version 3.1.1</div>
</table></div></address></body>
</html>