1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
<!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 - listboxcombo/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></a>
</div>
</table>
<h1 align=center>Listboxes and Comboboxes</h1><br clear="all">
This example program demonstrates how to use Listboxes (with single selection
and multi selection) and Comboboxes (editable and non-editable).
<hr>
Header file: <pre>/****************************************************************************
** $Id: qt/examples/listboxcombo/listboxcombo.h 2.3.1 edited 2001-01-26 $
**
** 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_COMBO_H
#define LISTBOX_COMBO_H
#include <<a href="qvbox-h.html">qvbox.h</a>>
class QListBox;
class QLabel;
class ListBoxCombo : public QVBox
{
Q_OBJECT
public:
ListBoxCombo( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
protected:
<a href="qlistbox.html">QListBox</a> *lb1, *lb2;
<a href="qlabel.html">QLabel</a> *label1, *label2;
protected slots:
void slotLeft2Right();
void slotCombo1Activated( const QString &s );
void slotCombo2Activated( const QString &s );
};
#endif
</pre>
<hr>
Implementation: <pre>/****************************************************************************
** $Id: qt/examples/listboxcombo/listboxcombo.cpp 2.3.1 edited 2001-01-26 $
**
** 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 "listboxcombo.h"
#include <<a href="qcombobox-h.html">qcombobox.h</a>>
#include <<a href="qlistbox-h.html">qlistbox.h</a>>
#include <<a href="qhbox-h.html">qhbox.h</a>>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
#include <<a href="qstring-h.html">qstring.h</a>>
#include <<a href="qpixmap-h.html">qpixmap.h</a>>
#include <<a href="qlabel-h.html">qlabel.h</a>>
#include <<a href="qimage-h.html">qimage.h</a>>
class MyListBoxItem : public QListBoxItem
{
public:
MyListBoxItem()
: <a href="qlistboxitem.html">QListBoxItem</a>()
{
setCustomHighlighting( TRUE );
}
protected:
virtual void paint( <a href="qpainter.html">QPainter</a> * );
virtual int width( const QListBox* ) const { return 100; }
virtual int height( const QListBox* ) const { return 16; }
};
void <a name="376"></a>MyListBoxItem::paint( <a href="qpainter.html">QPainter</a> *painter )
{
// evil trick: find out whether we are painted onto our listbox
bool in_list_box = listBox() && listBox()->viewport() == painter-><a href="qpainter.html#84abcf">device</a>();
<a href="qrect.html">QRect</a> r ( 0, 0, <a href="qlistboxitem.html#8ff2cf">width</a>( listBox() ), <a href="qlistboxitem.html#bf15d8">height</a>( listBox() ) );
if ( in_list_box && selected() )
painter-><a href="qpainter.html#aa2595">eraseRect</a>( r );
painter-><a href="qpainter.html#0f4c23">fillRect</a>( 5, 5, <a href="qlistboxitem.html#8ff2cf">width</a>( listBox() ) - 10, <a href="qlistboxitem.html#bf15d8">height</a>( listBox() ) - 10, Qt::red );
if ( in_list_box && current() )
<a href="qlistboxitem.html#476e10">listBox</a>()->style().drawFocusRect( painter, r, <a href="qlistboxitem.html#476e10">listBox</a>()->colorGroup(), &painter-><a href="qpainter.html#09f5ca">backgroundColor</a>(), TRUE );
}
/*
* Constructor
*
* Creates child widgets of the ListBoxCombo widget
*/
ListBoxCombo::ListBoxCombo( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qvbox.html">QVBox</a>( parent, name )
{
<a href="qframe.html#ab991e">setMargin</a>( 5 );
<a href="qhbox.html#d8c6a6">setSpacing</a>( 5 );
unsigned int i;
<a href="qstring.html">QString</a> str;
<a href="qhbox.html">QHBox</a> *row1 = new <a href="qhbox.html">QHBox</a>( this );
row1-><a href="qhbox.html#d8c6a6">setSpacing</a>( 5 );
// Create a multi-selection ListBox...
lb1 = new <a href="qlistbox.html">QListBox</a>( row1 );
lb1->setSelectionMode( QListBox::Multi );
// ...insert a pixmap item...
lb1->insertItem( <a href="qpixmap.html">QPixmap</a>( "qtlogo.png" ) );
// ...and 100 text items
for ( i = 0; i < 100; i++ ) {
str = QString( "Listbox Item %1" ).arg( i );
if ( !( i % 4 ) )
lb1->insertItem( <a href="qpixmap.html">QPixmap</a>( "fileopen.xpm" ), str );
else
lb1->insertItem( str );
}
// Create a pushbutton...
<a href="qpushbutton.html">QPushButton</a> *arrow1 = new <a href="qpushbutton.html">QPushButton</a>( " -> ", row1 );
// ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
<a href="qobject.html#c2d209">connect</a>( arrow1, SIGNAL( clicked() ), this, SLOT( <a href=#377>slotLeft2Right</a>() ) );
// create an empty single-selection ListBox
lb2 = new <a href="qlistbox.html">QListBox</a>( row1 );
<a href="qhbox.html">QHBox</a> *row2 = new <a href="qhbox.html">QHBox</a>( this );
row2-><a href="qhbox.html#d8c6a6">setSpacing</a>( 5 );
<a href="qvbox.html">QVBox</a> *box1 = new <a href="qvbox.html">QVBox</a>( row2 );
box1-><a href="qhbox.html#d8c6a6">setSpacing</a>( 5 );
// Create a non-editable Combobox and a label below...
<a href="qcombobox.html">QComboBox</a> *cb1 = new <a href="qcombobox.html">QComboBox</a>( FALSE, box1 );
label1 = new <a href="qlabel.html">QLabel</a>( "Current Item: Combobox Item 0", box1 );
label1->setMaximumHeight( label1->sizeHint().height() * 2 );
label1->setFrameStyle( QFrame::Panel | QFrame::Sunken );
//...and insert 50 items into the Combobox
for ( i = 0; i < 50; i++ ) {
str = QString( "Combobox Item %1" ).arg( i );
if ( i % 9 )
cb1-><a href="qcombobox.html#bd8981">insertItem</a>( str );
else
cb1-><a href="qcombobox.html#0c879c">listBox</a>()->insertItem( new MyListBoxItem );
}
<a href="qvbox.html">QVBox</a> *box2 = new <a href="qvbox.html">QVBox</a>( row2 );
box2-><a href="qhbox.html#d8c6a6">setSpacing</a>( 5 );
// Create an editable Combobox and a label below...
<a href="qcombobox.html">QComboBox</a> *cb2 = new <a href="qcombobox.html">QComboBox</a>( TRUE, box2 );
label2 = new <a href="qlabel.html">QLabel</a>( "Current Item: Combobox Item 0", box2 );
label2->setMaximumHeight( label2->sizeHint().height() * 2 );
label2->setFrameStyle( QFrame::Panel | QFrame::Sunken );
// ... and insert 50 items into the Combobox
for ( i = 0; i < 50; i++ ) {
str = QString( "Combobox Item %1" ).arg( i );
if ( !( i % 4 ) )
cb2-><a href="qcombobox.html#bd8981">insertItem</a>( <a href="qpixmap.html">QPixmap</a>( "fileopen.xpm" ), str );
else
cb2-><a href="qcombobox.html#bd8981">insertItem</a>( str );
}
// Connect the activated SIGNALs of the Comboboxes with SLOTs
<a href="qobject.html#c2d209">connect</a>( cb1, SIGNAL( activated( const QString & ) ), this, SLOT( <a href=#378>slotCombo1Activated</a>( const QString & ) ) );
<a href="qobject.html#c2d209">connect</a>( cb2, SIGNAL( activated( const QString & ) ), this, SLOT( <a href=#379>slotCombo2Activated</a>( const QString & ) ) );
}
/*
* SLOT slotLeft2Right
*
* Copies all selected items of the first ListBox into the
* second ListBox
*/
void <a name="377"></a>ListBoxCombo::slotLeft2Right()
{
// Go through all items of the first ListBox
for ( unsigned int i = 0; i < lb1->count(); i++ ) {
<a href="qlistboxitem.html">QListBoxItem</a> *item = lb1->item( i );
// if the item is selected...
if ( item-><a href="qlistboxitem.html#648c07">selected</a>() ) {
// ...and it is a text item...
if ( item-><a href="qlistboxitem.html#ece6c3">pixmap</a>() && !item-><a href="qlistboxitem.html#56f62d">text</a>().isEmpty() )
lb2->insertItem( *item-><a href="qlistboxitem.html#ece6c3">pixmap</a>(), item-><a href="qlistboxitem.html#56f62d">text</a>() );
else if ( !item-><a href="qlistboxitem.html#ece6c3">pixmap</a>() )
lb2->insertItem( item-><a href="qlistboxitem.html#56f62d">text</a>() );
else if ( item-><a href="qlistboxitem.html#56f62d">text</a>().isEmpty() )
lb2->insertItem( *item-><a href="qlistboxitem.html#ece6c3">pixmap</a>() );
}
}
}
/*
* SLOT slotCombo1Activated( const QString &s )
*
* Sets the text of the item which the user just selected
* in the first Combobox (and is now the value of s) to
* the first Label.
*/
void <a name="378"></a>ListBoxCombo::slotCombo1Activated( const QString &s )
{
label1->setText( <a href="qstring.html">QString</a>( "Current Item: %1" ).arg( s ) );
}
/*
* SLOT slotCombo2Activated( const QString &s )
*
* Sets the text of the item which the user just selected
* in the second Combobox (and is now the value of s) to
* the second Label.
*/
void <a name="379"></a>ListBoxCombo::slotCombo2Activated( const QString &s )
{
label2->setText( <a href="qstring.html">QString</a>( "Current Item: %1" ).arg( s ) );
}
</pre>
<hr>
Main:
<pre>/****************************************************************************
** $Id: qt/examples/listboxcombo/main.cpp 2.3.1 edited 2001-01-26 $
**
** 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 "listboxcombo.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 );
ListBoxCombo listboxcombo;
listboxcombo.<a name="resize"></a><a href="qwidget.html#9ff9d8">resize</a>( 400, 270 );
listboxcombo.<a name="setCaption"></a><a href="qwidget.html#d4f007">setCaption</a>( "Examples for Listboxes and Comboboxes" );
a.<a name="setMainWidget"></a><a href="qapplication.html#11c921">setMainWidget</a>( &listboxcombo );
listboxcombo.<a name="show"></a><a href="qwidget.html#ccc8f7">show</a>();
return a.<a name="exec"></a><a href="qapplication.html#7076af">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>
|