Qt logo

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

Protected Members


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( QString 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 ( QListBox * listbox = 0 )

Constructs an empty list box item in the listbox listbox

QListBoxItem::~QListBoxItem () [virtual]

Destroys the list box item.

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

Implement this function to return the height of your item. The lb parameter is the same as listBox() and is provided for convenience and compatibility.

See also: paint() and width().

Reimplemented in QListBoxText and QListBoxPixmap.

QListBox * QListBoxItem::listBox () const

Returns a pointer to the listbox containing this item.

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 QString & text ) [virtual 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().

QString QListBoxItem::text () const [virtual]

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

See also: setText().


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.


Copyright İ 1999 Troll TechTrademarks
Qt version 2.0.2