📄 checklists-main-cpp.html
字号:
<!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>Qt Toolkit - checklists/main.cpp example file</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: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Listviews with Checkable Items</h1><br clear="all"> This example program shows how to use Listviews with different types of checkable Items. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/checklists/checklists.h 2.3.8 edited 2004-05-12 $**** 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 <<a href="qwidget-h.html">qwidget.h</a>>class QListView;class QLabel;class CheckLists : public QWidget{ Q_OBJECTpublic: 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> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/checklists/checklists.cpp 2.3.8 edited 2004-05-12 $**** 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 <<a href="qlistview-h.html">qlistview.h</a>>#include <<a href="qvbox-h.html">qvbox.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qvaluelist-h.html">qvaluelist.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>/* * Constructor * * Create all child widgets of the CheckList Widget */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 ); lay-><a href="qlayout.html#cc3fec">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-><a href="qlayout.html#cc3fec">setMargin</a>( 5 ); // First child: a Label vbox1-><a href="qboxlayout.html#ebba99">addWidget</a>( new QLabel( "Check some items!", this ) ); // Second child: the ListView lv1 = new <a href="qlistview.html">QListView</a>( this ); vbox1-><a href="qboxlayout.html#ebba99">addWidget</a>( lv1 ); lv1->addColumn( "Items" ); lv1->setRootIsDecorated( TRUE ); // create a list with 4 ListViewItems which will be parent items of other ListViewItems <a href="qvaluelist.html">QValueList</a><<a href="qlistviewitem.html">QListViewItem</a> *> parentList; parentList.<a href="qvaluelist.html#d68b11">append</a>( new QListViewItem( lv1, "Parent Item 1" ) ); parentList.<a href="qvaluelist.html#d68b11">append</a>( new QListViewItem( lv1, "Parent Item 2" ) ); parentList.<a href="qvaluelist.html#d68b11">append</a>( new QListViewItem( lv1, "Parent Item 3" ) ); parentList.<a href="qvaluelist.html#d68b11">append</a>( new QListViewItem( lv1, "Parent Item 4" ) ); <a href="qlistviewitem.html">QListViewItem</a> *item = 0; unsigned int num = 1; // go through the list of parent items... for ( <a href="qvaluelist.html">QValueList</a><<a href="qlistviewitem.html">QListViewItem</a>*>::Iterator it = parentList.<a href="qvaluelist.html#6372f0">begin</a>(); it != parentList.<a href="qvaluelist.html#4996b7">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="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-><a href="qlayout.html#cc3fec">setMargin</a>( 5 ); // create a pushbutton <a href="qpushbutton.html">QPushButton</a> *copy1 = new <a href="qpushbutton.html">QPushButton</a>( " -> ", this ); tmp-><a href="qboxlayout.html#ebba99">addWidget</a>( copy1 ); copy1-><a href="qwidget.html#99d237">setMaximumWidth</a>( copy1-><a href="qpushbutton.html#53cdcb">sizeHint</a>().width() ); // connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2() <a href="qobject.html#fbde73">connect</a>( copy1, SIGNAL( clicked() ), this, SLOT( <a href=#229>copy1to2</a>() ) ); // another widget for layouting <a href="qvboxlayout.html">QVBoxLayout</a> *vbox2 = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay ); vbox2-><a href="qlayout.html#cc3fec">setMargin</a>( 5 ); // and another label vbox2-><a href="qboxlayout.html#ebba99">addWidget</a>( new QLabel( "Check one item!", this ) ); // create the second listview lv2 = new <a href="qlistview.html">QListView</a>( this ); vbox2-><a href="qboxlayout.html#ebba99">addWidget</a>( lv2 ); lv2->addColumn( "Items" ); lv2->setRootIsDecorated( TRUE ); // another widget needed for layouting only tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay ); tmp-><a href="qlayout.html#cc3fec">setMargin</a>( 5 ); // create another pushbutton... <a href="qpushbutton.html">QPushButton</a> *copy2 = new <a href="qpushbutton.html">QPushButton</a>( " -> ", this ); lay-><a href="qboxlayout.html#ebba99">addWidget</a>( copy2 ); copy2-><a href="qwidget.html#99d237">setMaximumWidth</a>( copy2-><a href="qpushbutton.html#53cdcb">sizeHint</a>().width() ); // ...and connect its clicked() SIGNAL to the copy2to3() SLOT <a href="qobject.html#fbde73">connect</a>( copy2, SIGNAL( clicked() ), this, SLOT( <a href=#230>copy2to3</a>() ) ); tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay ); tmp-><a href="qlayout.html#cc3fec">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-><a href="qboxlayout.html#ebba99">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="229"></a>CheckLists::copy1to2(){ // create an iterator which operates on the first ListView <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv1 ); lv2->clear(); // 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 ); item-><a href="qlistviewitem.html#1c5a28">setOpen</a>( TRUE ); // iterate through the first ListView... for ( ; it.<a href="qlistviewitemiterator.html#b134cd">current</a>(); ++it ) // ...check state of childs, and... if ( it.<a href="qlistviewitemiterator.html#b134cd">current</a>()->parent() ) // ...if the item is checked... if ( ( (<a href="qchecklistitem.html">QCheckListItem</a>*)it.<a href="qlistviewitemiterator.html#b134cd">current</a>() )->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#b134cd">current</a>()->text( 0 ), QCheckListItem::RadioButton ); if ( item-><a href="qlistviewitem.html#ce655c">firstChild</a>() ) ( ( <a href="qchecklistitem.html">QCheckListItem</a>* )item-><a href="qlistviewitem.html#ce655c">firstChild</a>() )->setOn( TRUE );}/* * SLOT copy2to3() * * Copies the checked item of the second ListView into the * Label at the right. */void <a name="230"></a>CheckLists::copy2to3(){ // create an iterator which operates on the second ListView <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv2 ); label->setText( "No Item checked" ); // iterate through the second ListView... for ( ; it.<a href="qlistviewitemiterator.html#b134cd">current</a>(); ++it ) // ...check state of childs, and... if ( it.<a href="qlistviewitemiterator.html#b134cd">current</a>()->parent() ) // ...if the item is checked... if ( ( (<a href="qchecklistitem.html">QCheckListItem</a>*)it.<a href="qlistviewitemiterator.html#b134cd">current</a>() )->isOn() ) // ...set the text of the item to the label label->setText( it.<a href="qlistviewitemiterator.html#b134cd">current</a>()->text( 0 ) );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/checklists/main.cpp 2.3.8 edited 2004-05-12 $**** 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); CheckLists checklists; checklists.<a name="resize"></a><a href="qwidget.html#8fcbbe">resize</a>( 650, 350 ); checklists.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - CheckLists" ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &checklists ); checklists.<a name="show"></a><a href="qwidget.html#200ee5">show</a>(); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -