Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 773

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/listboxcombo/listboxcombo.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Listboxes and Comboboxes</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>Listboxes and Comboboxes</h1>

   
<p> 
This example program demonstrates how to use listboxes (with single selection
and multi selection) and comboboxes (editable and non-editable).
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id:  qt/listboxcombo.h   3.0.2   edited Oct 12 12:18 $
**
** 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 &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;

class QListBox;
class QLabel;

class ListBoxCombo : public <a href="qvbox.html">QVBox</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

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 <a href="qstring.html">QString</a> &amp;s );
    void slotCombo2Activated( const <a href="qstring.html">QString</a> &amp;s );

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id:  qt/listboxcombo.cpp   3.0.2   edited Oct 12 12:18 $
**
** 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 &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
#include &lt;<a href="qlistbox-h.html">qlistbox.h</a>&gt;
#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qstyle-h.html">qstyle.h</a>&gt;


class MyListBoxItem : public <a href="qlistboxitem.html">QListBoxItem</a>
{
public:
    MyListBoxItem()
        : <a href="qlistboxitem.html">QListBoxItem</a>()
    {
        setCustomHighlighting( TRUE );
    }

protected:
    virtual void paint( <a href="qpainter.html">QPainter</a> * );
    virtual int width( const <a href="qlistbox.html">QListBox</a>* ) const { return 100; }
    virtual int height( const <a href="qlistbox.html">QListBox</a>* ) const { return 16; }

};

<a name="x1250"></a>void MyListBoxItem::<a href="qlistboxitem.html#paint">paint</a>( <a href="qpainter.html">QPainter</a> *painter )
{
    // evil trick: find out whether we are painted onto our listbox
<a name="x1253"></a>    bool in_list_box = <a href="qlistboxitem.html#listBox">listBox</a>() &amp;&amp; listBox()-&gt;viewport() == painter-&gt;<a href="qpainter.html#device">device</a>();

    <a href="qrect.html">QRect</a> r ( 0, 0, width( <a href="qlistboxitem.html#listBox">listBox</a>() ), height( <a href="qlistboxitem.html#listBox">listBox</a>() ) );
    if ( in_list_box &amp;&amp; isSelected() )
<a name="x1254"></a>        painter-&gt;<a href="qpainter.html#eraseRect">eraseRect</a>( r );
<a name="x1255"></a>    painter-&gt;<a href="qpainter.html#fillRect">fillRect</a>( 5, 5, width( listBox() ) - 10, height( listBox() ) - 10, Qt::red );
    if ( in_list_box &amp;&amp; isCurrent() )
        listBox()-&gt;style().drawPrimitive( QStyle::PE_FocusRect, painter, r, listBox()-&gt;colorGroup() );
}

/*
 * Constructor
 *
 * Creates child widgets of the ListBoxCombo widget
 */

<a name="f381"></a>ListBoxCombo::ListBoxCombo( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qvbox.html">QVBox</a>( parent, name )
{
    <a href="qframe.html#setMargin">setMargin</a>( 5 );
    <a href="qhbox.html#setSpacing">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 );
<a name="x1243"></a>    row1-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>( 5 );

    // Create a multi-selection ListBox...
    lb1 = new <a href="qlistbox.html">QListBox</a>( row1 );
<a name="x1248"></a>    lb1-&gt;<a href="qlistbox.html#setSelectionMode">setSelectionMode</a>( QListBox::Multi );

    // ...insert a pixmap item...
<a name="x1246"></a>    lb1-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( QPixmap( "qtlogo.png" ) );
    // ...and 100 text items
    for ( i = 0; i &lt; 100; i++ ) {
        str = QString( "Listbox Item %1" ).arg( i );
        if ( !( i % 4 ) )
            lb1-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( QPixmap( "fileopen.xpm" ), str );
        else
            lb1-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( str );
    }

    // Create a pushbutton...
    <a href="qpushbutton.html">QPushButton</a> *arrow1 = new <a href="qpushbutton.html">QPushButton</a>( " -&gt; ", row1 );
    // ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
    <a href="qobject.html#connect">connect</a>( arrow1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotLeft2Right() ) );

    // 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-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>( 5 );

    <a href="qvbox.html">QVBox</a> *box1 = new <a href="qvbox.html">QVBox</a>( row2 );
    box1-&gt;<a href="qhbox.html#setSpacing">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 );
<a name="x1256"></a>    label1-&gt;<a href="qwidget.html#setMaximumHeight">setMaximumHeight</a>( label1-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().height() * 2 );
<a name="x1242"></a>    label1-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );

    //...and insert 50 items into the Combobox
    for ( i = 0; i &lt; 50; i++ ) {
        str = QString( "Combobox Item %1" ).arg( i );
        if ( i % 9 )
<a name="x1240"></a>            cb1-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( str );
        else
<a name="x1241"></a>            cb1-&gt;<a href="qcombobox.html#listBox">listBox</a>()-&gt;insertItem( new MyListBoxItem );
    }

    <a href="qvbox.html">QVBox</a> *box2 = new <a href="qvbox.html">QVBox</a>( row2 );
    box2-&gt;<a href="qhbox.html#setSpacing">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-&gt;<a href="qwidget.html#setMaximumHeight">setMaximumHeight</a>( label2-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().height() * 2 );
    label2-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );

    // ... and insert 50 items into the Combobox
    for ( i = 0; i &lt; 50; i++ ) {
        str = QString( "Combobox Item %1" ).arg( i );
        if ( !( i % 4 ) )
            cb2-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( "fileopen.xpm" ), str );
        else
            cb2-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( str );
    }

    // Connect the activated SIGNALs of the Comboboxes with SLOTs
<a name="x1239"></a>    <a href="qobject.html#connect">connect</a>( cb1, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> &amp; ) ), this, SLOT( slotCombo1Activated( const <a href="qstring.html">QString</a> &amp; ) ) );
    <a href="qobject.html#connect">connect</a>( cb2, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> &amp; ) ), this, SLOT( slotCombo2Activated( const <a href="qstring.html">QString</a> &amp; ) ) );
}

/*
 * SLOT slotLeft2Right
 *
 * Copies all selected items of the first ListBox into the
 * second ListBox
 */

void <a name="f382"></a>ListBoxCombo::slotLeft2Right()
{
    // Go through all items of the first ListBox
<a name="x1245"></a>    for ( unsigned int i = 0; i &lt; lb1-&gt;<a href="qlistbox.html#count">count</a>(); i++ ) {
<a name="x1247"></a>        <a href="qlistboxitem.html">QListBoxItem</a> *item = lb1-&gt;<a href="qlistbox.html#item">item</a>( i );
        // if the item is selected...
<a name="x1249"></a>        if ( item-&gt;<a href="qlistboxitem.html#isSelected">isSelected</a>() ) {
            // ...and it is a text item...
<a name="x1252"></a><a name="x1251"></a>            if ( item-&gt;<a href="qlistboxitem.html#pixmap">pixmap</a>() &amp;&amp; !item-&gt;<a href="qlistboxitem.html#text">text</a>().isEmpty() )
                lb2-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( *item-&gt;<a href="qlistboxitem.html#pixmap">pixmap</a>(), item-&gt;<a href="qlistboxitem.html#text">text</a>() );
            else if ( !item-&gt;<a href="qlistboxitem.html#pixmap">pixmap</a>() )
                lb2-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( item-&gt;<a href="qlistboxitem.html#text">text</a>() );
            else if ( item-&gt;<a href="qlistboxitem.html#text">text</a>().isEmpty() )
                lb2-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( *item-&gt;<a href="qlistboxitem.html#pixmap">pixmap</a>() );
        }
    }
}

/*
 * SLOT slotCombo1Activated( const <a href="qstring.html">QString</a> &amp;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="f383"></a>ListBoxCombo::slotCombo1Activated( const <a href="qstring.html">QString</a> &amp;s )
{
    label1-&gt;<a href="qlabel.html#setText">setText</a>( QString( "Current Item: %1" ).arg( s ) );
}

/*
 * SLOT slotCombo2Activated( const <a href="qstring.html">QString</a> &amp;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="f384"></a>ListBoxCombo::slotCombo2Activated( const <a href="qstring.html">QString</a> &amp;s )
{
    label2-&gt;<a href="qlabel.html#setText">setText</a>( QString( "Current Item: %1" ).arg( s ) );
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/main.cpp   3.0.2   edited Oct 12 12:18 $
**
** 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 &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 );

    ListBoxCombo listboxcombo;
    listboxcombo.<a href="qwidget.html#resize">resize</a>( 400, 270 );
    listboxcombo.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Listboxes and Comboboxes" );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;listboxcombo );
    listboxcombo.<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; 2001 
<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.0.2</div>
</table></div></address></body>
</html>