Qt Toolkit - QListBoxItem Class

# QListBoxItem Class Reference

This is the base class of all list box items. More...

`#include <qlistbox.h>`

Inherited by QListBoxPixmap and QListBoxText.

List of all member functions.

## Public Members

- QListBoxItem ()
- virtual ~QListBoxItem ()
- virtual const char* text () const
- virtual const QPixmap* pixmap () const
- virtual int height ( const QListBox * ) const
- virtual int width ( const QListBox * ) const

## Protected Members

- virtual void paint ( QPainter * )
- void setText ( const char * text )

## Detailed Description

This is the base class of all list box items.

This class is the abstract base class of all list box items. If you need to insert customized items into a QListBox, you must inherit this class and reimplement paint(), height() and width().

The following shows how to define a list box item which shows a pixmap and a text:

        class MyListBoxItem : public QListBoxItem
        {
        public:
            MyListBoxItem( const char *s, const QPixmap p )
                : QListBoxItem(), pm(p)
                { setText( s ); }

        protected:
            virtual void paint( QPainter * );
            virtual int height( const QListBox * ) const;
            virtual int width( const QListBox * ) const;
            virtual const QPixmap *pixmap() { return &pm; }

        private:
            QPixmap pm;
        };

        void MyListBoxItem::paint( QPainter *p )
        {
            p->drawPixmap( 3, 0, pm );
            QFontMetrics fm = p->fontMetrics();
            int yPos;                       // vertical text position
            if ( pm.height() < fm.height() )
                yPos = fm.ascent() + fm.leading()/2;
            else
                yPos = pm.height()/2 - fm.height()/2 + fm.ascent();
            p->drawText( pm.width() + 5, yPos, text() );
        }

        int MyListBoxItem::height(const QListBox *lb ) const
        {
            return QMAX( pm.height(), lb->fontMetrics().lineSpacing() + 1 );
        }

        int MyListBoxItem::width(const QListBox *lb ) const
        {
            return pm.width() + lb->fontMetrics().width( text() ) + 6;
        }

See also: QListBox.

## Member Function Documentation

### QListBoxItem::QListBoxItem ()

Constructs an empty list box item.

### QListBoxItem::~QListBoxItem () `[virtual]`

Destroys the list box item.

### int QListBoxItem::height ( const QListBox * ) const `[virtual]`

Implement this function to return the height of your item

See also: paint() and width().

Reimplemented in QListBoxPixmap and QListBoxText.

### void QListBoxItem::paint ( QPainter * p ) `[virtual protected]`

Implement this function to draw your item.

See also: height() and width().

Reimplemented in QListBoxPixmap and QListBoxText.

### const QPixmap * QListBoxItem::pixmap () const `[virtual]`

Returns the pixmap connected with the item, if any.

The default implementation of this function returns a null pointer.

Reimplemented in QListBoxPixmap.

### void QListBoxItem::setText ( const char * text ) `[protected]`

Sets the text of the widget, which is used for sorting. The text is not shown unless explicitly drawn in paint().

See also: text().

### const char * QListBoxItem::text () const `[virtual]`

Returns the text of the item, which is used for sorting.

See also: setText().

### int QListBoxItem::width ( const QListBox * ) const `[virtual]`

Implement this function to return the width of your item

See also: paint() and height().

Reimplemented in QListBoxPixmap and QListBoxText.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qlistbox.h: 1998/08/21
- qlistbox.cpp: 1999/02/13