📄 listbox-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 - listbox/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>Listboxe Example</h1><br clear="all"> This example shows how to use the different modes (single columns, multipe columns, fixed number of rows, etc.) of QListBox. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/listbox/listbox.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 LISTBOX_H#define LISTBOX_Hclass QSpinBox;class QListBox;class QButtonGroup;#include <<a href="qwidget-h.html">qwidget.h</a>>class ListBoxDemo: public QWidget{ Q_OBJECTpublic: ListBoxDemo(); ~ListBoxDemo();private slots: void setNumRows(); void setNumCols(); void setRowsByHeight(); void setColsByWidth(); void setVariableWidth( bool ); void setVariableHeight( bool ); void setMultiSelection( bool ); void sortAscending(); void sortDescending();private: <a href="qlistbox.html">QListBox</a> * l; <a href="qspinbox.html">QSpinBox</a> * columns; <a href="qspinbox.html">QSpinBox</a> * rows; <a href="qbuttongroup.html">QButtonGroup</a> * bg;};#endif</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/listbox/listbox.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 "listbox.h"#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qradiobutton-h.html">qradiobutton.h</a>>#include <<a href="qcheckbox-h.html">qcheckbox.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>#include <<a href="qlistbox-h.html">qlistbox.h</a>>#include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>ListBoxDemo::ListBoxDemo() : <a href="qwidget.html">QWidget</a>( 0, 0 ){ <a href="qgridlayout.html">QGridLayout</a> * g = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 2, 6 ); g-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "<b>Configuration:</b>", this ), 0, 0 ); g-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "<b>Result:</b>", this ), 0, 1 ); l = new <a href="qlistbox.html">QListBox</a>( this ); g-><a href="qgridlayout.html#dac29c">addWidget</a>( l, 1, 1 ); l->setFocusPolicy( QWidget::StrongFocus ); <a href="qvboxlayout.html">QVBoxLayout</a> * v = new <a href="qvboxlayout.html">QVBoxLayout</a>; g-><a href="qgridlayout.html#a409bc">addLayout</a>( v, 1, 0 ); <a href="qradiobutton.html">QRadioButton</a> * b; bg = new <a href="qbuttongroup.html">QButtonGroup</a>( 0 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of columns,\n" "as many rows as needed.", this ); bg->insert( b ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( b ); b-><a href="qradiobutton.html#0e50fc">setChecked</a>( TRUE ); <a href="qobject.html#fbde73">connect</a>( b, SIGNAL(clicked()), this, SLOT(<a href=#67>setNumCols</a>()) ); <a href="qhboxlayout.html">QHBoxLayout</a> * h = new <a href="qhboxlayout.html">QHBoxLayout</a>; v-><a href="qboxlayout.html#6ff301">addLayout</a>( h ); h-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 30 ); h-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 100 ); h-><a href="qboxlayout.html#ebba99">addWidget</a>( new QLabel( "Columns:", this ) ); columns = new <a href="qspinbox.html">QSpinBox</a>( this ); h-><a href="qboxlayout.html#ebba99">addWidget</a>( columns ); v-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "As many columns as fit on-screen,\n" "as many rows as needed.", this ); bg->insert( b ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( b ); <a href="qobject.html#fbde73">connect</a>( b, SIGNAL(clicked()), this, SLOT(<a href=#69>setColsByWidth</a>()) ); v-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of rows,\n" "as many columns as needed.", this ); bg->insert( b ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( b ); <a href="qobject.html#fbde73">connect</a>( b, SIGNAL(clicked()), this, SLOT(<a href=#66>setNumRows</a>()) ); h = new <a href="qhboxlayout.html">QHBoxLayout</a>; v-><a href="qboxlayout.html#6ff301">addLayout</a>( h ); h-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 30 ); h-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 100 ); h-><a href="qboxlayout.html#ebba99">addWidget</a>( new QLabel( "Rows:", this ) ); rows = new <a href="qspinbox.html">QSpinBox</a>( this ); rows->setEnabled( FALSE ); h-><a href="qboxlayout.html#ebba99">addWidget</a>( rows ); v-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "As many rows as fit on-screen,\n" "as many columns as needed.", this ); bg->insert( b ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( b ); <a href="qobject.html#fbde73">connect</a>( b, SIGNAL(clicked()), this, SLOT(<a href=#68>setRowsByHeight</a>()) ); v-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 12 ); <a href="qcheckbox.html">QCheckBox</a> * cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-height rows", this ); cb-><a href="qcheckbox.html#08b09c">setChecked</a>( TRUE ); <a href="qobject.html#fbde73">connect</a>( cb, SIGNAL(toggled(bool)), this, SLOT(<a href=#71>setVariableHeight</a>(bool)) ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( cb ); v-><a href="qboxlayout.html#fc3fb6">addSpacing</a>( 6 ); cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-width columns", this ); <a href="qobject.html#fbde73">connect</a>( cb, SIGNAL(toggled(bool)), this, SLOT(<a href=#70>setVariableWidth</a>(bool)) ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( cb ); cb = new <a href="qcheckbox.html">QCheckBox</a>( "Extended-Selection", this ); <a href="qobject.html#fbde73">connect</a>( cb, SIGNAL(toggled(bool)), this, SLOT(<a href=#72>setMultiSelection</a>(bool)) ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( cb ); <a href="qpushbutton.html">QPushButton</a> *pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort ascending", this ); <a href="qobject.html#fbde73">connect</a>( pb, SIGNAL( clicked() ), this, SLOT( <a href=#73>sortAscending</a>() ) ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( pb ); pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort descending", this ); <a href="qobject.html#fbde73">connect</a>( pb, SIGNAL( clicked() ), this, SLOT( <a href=#74>sortDescending</a>() ) ); v-><a href="qboxlayout.html#ebba99">addWidget</a>( pb ); v-><a href="qboxlayout.html#0226eb">addStretch</a>( 100 ); int i = 0; while( ++i <= 2560 ) l->insertItem( <a href="qstring.html#0b1906">QString::fromLatin1</a>( "Item " ) + QString::number( i ), i ); columns->setRange( 1, 256 ); columns->setValue( 1 ); rows->setRange( 1, 256 ); rows->setValue( 256 ); <a href="qobject.html#fbde73">connect</a>( columns, SIGNAL(valueChanged(int)), this, SLOT(<a href=#67>setNumCols</a>()) ); <a href="qobject.html#fbde73">connect</a>( rows, SIGNAL(valueChanged(int)), this, SLOT(<a href=#66>setNumRows</a>()) );}ListBoxDemo::~ListBoxDemo(){ delete bg;}void <a name="66"></a>ListBoxDemo::setNumRows(){ columns->setEnabled( FALSE ); rows->setEnabled( TRUE ); l->setRowMode( rows->value() );}void <a name="67"></a>ListBoxDemo::setNumCols(){ columns->setEnabled( TRUE ); rows->setEnabled( FALSE ); l->setColumnMode( columns->value() );}void <a name="68"></a>ListBoxDemo::setRowsByHeight(){ columns->setEnabled( FALSE ); rows->setEnabled( FALSE ); l->setRowMode( QListBox::FitToHeight );}void <a name="69"></a>ListBoxDemo::setColsByWidth(){ columns->setEnabled( FALSE ); rows->setEnabled( FALSE ); l->setColumnMode( QListBox::FitToWidth );}void <a name="70"></a>ListBoxDemo::setVariableWidth( bool b ){ l->setVariableWidth( b );}void <a name="71"></a>ListBoxDemo::setVariableHeight( bool b ){ l->setVariableHeight( b );}void <a name="72"></a>ListBoxDemo::setMultiSelection( bool b ){ l->clearSelection(); l->setSelectionMode( b ? QListBox::Extended : QListBox::Single );}void <a name="73"></a>ListBoxDemo::sortAscending(){ l->sort( TRUE );}void <a name="74"></a>ListBoxDemo::sortDescending(){ l->sort( FALSE );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/listbox/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 "listbox.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 ); ListBoxDemo t; t.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Listbox" ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &t ); t.<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 + -