<!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 > Listviews with Checkable Items< / 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 > 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.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 CHECKLISTS_H
#define CHECKLISTS_H
#include < < a href = "tqwidget-h.html" > tqwidget.h< / a > >
class TQListView;
class TQLabel;
class CheckLists : public < a href = "tqwidget.html" > TQWidget< / a >
{
< a href = "metaobjects.html#TQ_OBJECT" > TQ_OBJECT< / a >
public:
CheckLists( < a href = "tqwidget.html" > TQWidget< / a > *parent = 0, const char *name = 0 );
protected:
< a href = "tqlistview.html" > TQListView< / a > *lv1, *lv2;
< a href = "tqlabel.html" > TQLabel< / a > *label;
protected slots:
void copy1to2();
void copy2to3();
};
#endif
< / pre >
< p > < hr >
< p > Implementation:
< p > < pre > /****************************************************************************
** $Id: qt/checklists.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 "checklists.h"
#include < < a href = "tqlistview-h.html" > tqlistview.h< / a > >
#include < < a href = "tqvbox-h.html" > tqvbox.h< / a > >
#include < < a href = "tqlabel-h.html" > tqlabel.h< / a > >
#include < < a href = "tqvaluelist-h.html" > tqvaluelist.h< / a > >
#include < < a href = "tqstring-h.html" > tqstring.h< / a > >
#include < < a href = "tqpushbutton-h.html" > tqpushbutton.h< / a > >
#include < < a href = "tqlayout-h.html" > tqlayout.h< / a > >
/*
* Constructor
*
* Create all child widgets of the CheckList Widget
*/
< a name = "f248" > < / a > CheckLists::CheckLists( < a href = "tqwidget.html" > TQWidget< / a > *parent, const char *name )
: < a href = "tqwidget.html" > TQWidget< / a > ( parent, name )
{
< a href = "tqhboxlayout.html" > TQHBoxLayout< / a > *lay = new < a href = "tqhboxlayout.html" > TQHBoxLayout< / a > ( this );
< a name = "x426" > < / a > lay-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// create a widget which layouts its childs in a column
< a href = "tqvboxlayout.html" > TQVBoxLayout< / a > *vbox1 = new < a href = "tqvboxlayout.html" > TQVBoxLayout< / a > ( lay );
vbox1-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// First child: a Label
< a name = "x423" > < / a > vbox1-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( new < a href = "tqlabel.html" > TQLabel< / a > ( "Check some items!", this ) );
// Second child: the ListView
lv1 = new < a href = "tqlistview.html" > TQListView< / a > ( this );
vbox1-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( lv1 );
< a name = "x427" > < / a > lv1-> < a href = "tqlistview.html#addColumn" > addColumn< / a > ( "Items" );
< a name = "x429" > < / a > lv1-> < a href = "tqlistview.html#setRootIsDecorated" > setRootIsDecorated< / a > ( TRUE );
// create a list with 4 ListViewItems which will be parent items of other ListViewItems
< a href = "tqvaluelist.html" > TQValueList< / a > < TQListViewItem *> parentList;
< a name = "x434" > < / a > parentList.< a href = "tqvaluelist.html#append" > append< / a > ( new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( lv1, "Parent Item 1", TQCheckListItem::CheckBoxController ) );
parentList.< a href = "tqvaluelist.html#append" > append< / a > ( new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( lv1, "Parent Item 2", TQCheckListItem::CheckBoxController ) );
parentList.< a href = "tqvaluelist.html#append" > append< / a > ( new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( lv1, "Parent Item 3", TQCheckListItem::CheckBoxController ) );
parentList.< a href = "tqvaluelist.html#append" > append< / a > ( new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( lv1, "Parent Item 4", TQCheckListItem::CheckBoxController ) );
< a href = "tqlistviewitem.html" > TQListViewItem< / a > *item = 0;
unsigned int num = 1;
// go through the list of parent items...
< a name = "x436" > < / a > < a name = "x435" > < / a > for ( TQValueList< TQListViewItem*> ::Iterator it = parentList.< a href = "tqvaluelist.html#begin" > begin< / a > (); it != parentList.< a href = "tqvaluelist.html#end" > end< / a > ();
( *it )-> setOpen( TRUE ), ++it, num++ ) {
item = *it;
// ...and create 5 checkable child ListViewItems for each parent item
for ( unsigned int i = 1; i < = 5; i++ )
(void)new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( item, TQString( "%1. Child of Parent %2" ).arg( i ).arg( num ), TQCheckListItem::CheckBox );
}
// Create another widget for layouting
< a href = "tqvboxlayout.html" > TQVBoxLayout< / a > *tmp = new < a href = "tqvboxlayout.html" > TQVBoxLayout< / a > ( lay );
tmp-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// create a pushbutton
< a href = "tqpushbutton.html" > TQPushButton< / a > *copy1 = new < a href = "tqpushbutton.html" > TQPushButton< / a > ( " -> ", this );
tmp-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( copy1 );
< a name = "x437" > < / a > < a name = "x433" > < / a > copy1-> < a href = "tqwidget.html#setMaximumWidth" > setMaximumWidth< / a > ( copy1-> < a href = "tqwidget.html#sizeHint" > sizeHint< / a > ().width() );
// connect the TQ_SIGNAL clicked() of the pushbutton with the TQ_SLOT copy1to2()
< a href = "tqobject.html#connect" > connect< / a > ( copy1, TQ_SIGNAL( < a href = "tqbutton.html#clicked" > clicked< / a > () ), this, TQ_SLOT( copy1to2() ) );
// another widget for layouting
< a href = "tqvboxlayout.html" > TQVBoxLayout< / a > *vbox2 = new < a href = "tqvboxlayout.html" > TQVBoxLayout< / a > ( lay );
vbox2-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// and another label
vbox2-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( new < a href = "tqlabel.html" > TQLabel< / a > ( "Check one item!", this ) );
// create the second listview
lv2 = new < a href = "tqlistview.html" > TQListView< / a > ( this );
vbox2-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( lv2 );
lv2-> < a href = "tqlistview.html#addColumn" > addColumn< / a > ( "Items" );
lv2-> < a href = "tqlistview.html#setRootIsDecorated" > setRootIsDecorated< / a > ( TRUE );
// another widget needed for layouting only
tmp = new < a href = "tqvboxlayout.html" > TQVBoxLayout< / a > ( lay );
tmp-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// create another pushbutton...
< a href = "tqpushbutton.html" > TQPushButton< / a > *copy2 = new < a href = "tqpushbutton.html" > TQPushButton< / a > ( " -> ", this );
lay-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( copy2 );
copy2-> < a href = "tqwidget.html#setMaximumWidth" > setMaximumWidth< / a > ( copy2-> < a href = "tqwidget.html#sizeHint" > sizeHint< / a > ().width() );
// ...and connect its clicked() TQ_SIGNAL to the copy2to3() TQ_SLOT
< a href = "tqobject.html#connect" > connect< / a > ( copy2, TQ_SIGNAL( < a href = "tqbutton.html#clicked" > clicked< / a > () ), this, TQ_SLOT( copy2to3() ) );
tmp = new < a href = "tqvboxlayout.html" > TQVBoxLayout< / a > ( lay );
tmp-> < a href = "tqlayout.html#setMargin" > setMargin< / a > ( 5 );
// and create a label which will be at the right of the window
label = new < a href = "tqlabel.html" > TQLabel< / a > ( "No Item yet...", this );
tmp-> < a href = "tqboxlayout.html#addWidget" > addWidget< / a > ( label );
}
/*
* TQ_SLOT copy1to2()
*
* Copies all checked ListViewItems from the first ListView to
* the second one, and inserts them as Radio-ListViewItem.
*/
void < a name = "f249" > < / a > CheckLists::copy1to2()
{
// create an iterator which operates on the first ListView
< a href = "tqlistviewitemiterator.html" > TQListViewItemIterator< / a > it( lv1 );
< a name = "x428" > < / a > lv2-> < a href = "tqlistview.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 = "tqchecklistitem.html" > TQCheckListItem< / a > *item = new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( lv2, "Controller", TQCheckListItem::Controller );
< a name = "x431" > < / a > item-> < a href = "tqlistviewitem.html#setOpen" > setOpen< / a > ( TRUE );
// iterate through the first ListView...
< a name = "x432" > < / a > for ( ; it.< a href = "tqlistviewitemiterator.html#current" > current< / a > (); ++it )
// ...check state of childs, and...
if ( it.< a href = "tqlistviewitemiterator.html#current" > current< / a > ()-> parent() )
// ...if the item is checked...
if ( ( (TQCheckListItem*)it.< a href = "tqlistviewitemiterator.html#current" > current< / a > () )-> isOn() )
// ...insert a Radio-ListViewItem with the same text into the second ListView
(void)new < a href = "tqchecklistitem.html" > TQCheckListItem< / a > ( item, it.< a href = "tqlistviewitemiterator.html#current" > current< / a > ()-> text( 0 ), TQCheckListItem::RadioButton );
< a name = "x430" > < / a > if ( item-> < a href = "tqlistviewitem.html#firstChild" > firstChild< / a > () )
( ( < a href = "tqchecklistitem.html" > TQCheckListItem< / a > * )item-> < a href = "tqlistviewitem.html#firstChild" > firstChild< / a > () )-> setOn( TRUE );
}
/*
* TQ_SLOT copy2to3()
*
* Copies the checked item of the second ListView into the
* Label at the right.
*/
void < a name = "f250" > < / a > CheckLists::copy2to3()
{
// create an iterator which operates on the second ListView
< a href = "tqlistviewitemiterator.html" > TQListViewItemIterator< / a > it( lv2 );
label-> < a href = "tqlabel.html#setText" > setText< / a > ( "No Item checked" );
// iterate through the second ListView...
for ( ; it.< a href = "tqlistviewitemiterator.html#current" > current< / a > (); ++it )
// ...check state of childs, and...
if ( it.< a href = "tqlistviewitemiterator.html#current" > current< / a > ()-> parent() )
// ...if the item is checked...
if ( ( (TQCheckListItem*)it.< a href = "tqlistviewitemiterator.html#current" > current< / a > () )-> isOn() )
// ...set the text of the item to the label
label-> < a href = "tqlabel.html#setText" > setText< / a > ( it.< a href = "tqlistviewitemiterator.html#current" > current< / a > ()-> text( 0 ) );
}
< / 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 "checklists.h"
#include < < a href = "tqapplication-h.html" > tqapplication.h< / a > >
int main( int argc, char **argv )
{
< a href = "tqapplication.html" > TQApplication< / a > a( argc, argv );
CheckLists checklists;
checklists.< a href = "tqwidget.html#resize" > resize< / a > ( 650, 350 );
checklists.< a href = "tqwidget.html#setCaption" > setCaption< / a > ( "TQt Example - CheckLists" );
a.< a href = "tqapplication.html#setMainWidget" > setMainWidget< / a > ( & checklists );
checklists.< a href = "tqwidget.html#show" > show< / a > ();
return a.< a href = "tqapplication.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 © 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 >