Qt logo

QListBox Class Reference


The QListBox widget provides a list of selectable, read-only items. More...

#include <qlistbox.h>

Inherits QScrollView.

Inherited by QFileListBox.

List of all member functions.

Public Members

Public Slots

Signals

Protected Members


Detailed Description

The QListBox widget provides a list of selectable, read-only items.

This is typically a single-column list where zero or one item items are selected at once, but can also be used in many other ways.

QListBox will add scroll bars as necessary, but isn't intended for really big lists. If you want more than a few thousand items, it's probably better to use a different widget, chiefly because the scroll bars won't provide very good navigation, but also because QListBox may become slow at larger sizes.

There is a variety of selection modes. The default is single-selection, but you can change it using setSelectionMode().

The list box normally arranges its items in a single column with a vertical scroll bar if necessary, but it is also possible to have a different fixed number of columns (setColumnMode()), or as many columns as will fit in the list box' assigned screen space (setColumnMode( FitToWidth )), or to have a fixed number of rows (setRowMode()), or as many rows as will fit in the list box' assigned screen space (setRowMode( FitToHeight )). In all these cases, QListBox will add scroll bars as appropriate in at least one direction.

If multiple rows is used, each row can be as high as necessary (the normal setting), or you can request that all items will have the same height by calling setVariableHeight( FALSE ). Of course there is a similar setVariableWidth().

The items discussed are QListBoxItem objects. QListBox provides methods to insert new items as a string, as pixmaps, and as QListBoxItem * (insertItem() with various arguments), and to replace an existing item with a new string, pixmap or QListBoxItem (changeItem() with various arguments). You can also remove items (surprise: removeItem()) and clear() the entire list box. Note that if you create a QListBoxItem yourself and insert it, it becomes the property of QListBox and you may not delete it. (QListBox will delete it when appropriate.)

You can also create a QListBoxItem such as QListBoxText or QListBoxPixmap with the list box as first parameter. The item will then append itself. When deleteting an item, it will automatically be remove from the listbox again.

The list of items can be arbitrarily big; if necessary, QListBox adds scroll bars. It can be single-column (as most list boxes are) or multi-column, and offers both single and multiple selection. (QListBox does however not support multiple-column items; QListView does that job.)

The list box items can be accessed both as QListBoxItem objects (recommended) and using integer indexes (the original QListBox implementation used an array of strings internally, and the API still supports this mode of operation). Everything can be done using the new objects; most things can be done using the indexes too but unfortunately not everything.

Each item in a QListBox contains a QListBoxItem. One of the items can be the current item. The highlighted() signal is emitted when a new item gets highlighted, e.g. because the user clicks on it or QListBox::setCurrentItem() is called. The selected() signal is emitted when the user double-clicks on an item or presses return when an item is highlighted.

If the user does not select anything, no signals are emitted and currentItem() returns -1.

A list box has WheelFocus as a default focusPolicy(), i.e. it can get keyboard focus both by tabbing, clicking and the mouse wheel.

New items may be inserted using either insertItem(), insertStrList() and inSort().

By default, vertical and horizontal scroll bars are added and removed as necessary. setAutoScrollBar() can be used to force a specific policy.

If you need to insert other types than texts and pixmaps, you must define new classes which inherit QListBoxItem.

Warning: The list box assumes ownership of all list box items and will delete them when is does not need them any more.

See also: QListView, QComboBox, QButtonGroup and GUI Design Handbook: List Box (two sections)

Examples: tabdialog/tabdialog.cpp


Member Type Documentation

QListBox::LayoutMode

This enum type decides how QListBox lays out its rows and columns. The two modes interact, of course.

The possible values for each mode are:

Example: When you call setRowMode( FitToHeight ), columnMode() automatically becomes Variable to accomodate the row mode you've set.

QListBox::SelectionMode

This enumerated type is used by QListBox to indicate how it reacts to selection by the user. It has three values:

In other words, Single is a real single-selection list box, Multi a real multi-selection list box, and Extended list box where users can select multiple items but usually want to select either just one or a range of contiguous items.


Member Function Documentation

QListBox::QListBox ( QWidget * parent=0, const char * name=0, WFlags f=0 )

Constructs a list box. The arguments are passed directly to the QScrollView constructor.

QListBox::~QListBox ()

Destroys the list box. Deletes all list box items.

void QListBox::changeItem ( const QListBoxItem * lbi, int index )

Replaces the item at position index with lbi. If index is negative or too large, changeItem() does nothing.

See also: insertItem() and removeItem().

void QListBox::changeItem ( const QPixmap & pixmap, int index )

Replaces the item at position index with pixmap.

The operation is ignored if index is out of range.

See also: insertItem() and removeItem().

void QListBox::changeItem ( const QString & text, int index )

Replaces the item at position index with text.

The operation is ignored if index is out of range.

See also: insertItem() and removeItem().

void QListBox::clear ()

Deletes all items in the list.

See also: removeItem() and setStrList().

void QListBox::clearSelection () [virtual slot]

Deselects all items, if possible.

Note that a single-selection listbox will automatically select an item if it has keyboard focus.

QListBox::LayoutMode QListBox::columnMode() const

Returns the column layout mode for this list box. This is normally FixedNumber, but can be changed by calling setColumnMode()

See also: rowMode(), setColumnMode() and numColumns().

uint QListBox::count () const

Returns the number of items in the list box.

int QListBox::currentItem () const

Returns the index of the current (highlighted) item of the list box, or -1 if no item has been selected.

See also: topItem().

void QListBox::doLayout () const [protected]

This function does the hard layout work. You should never need to call it.

void QListBox::ensureCurrentVisible () [virtual slot]

Ensures that the current item is visible.

void QListBox::focusInEvent ( QFocusEvent * ) [virtual protected]

Handles focus events. Repaints the current item (if not set, topItem() is made current).

See also: keyPressEvent() and focusOutEvent().

Reimplemented from QWidget.

void QListBox::focusOutEvent ( QFocusEvent * ) [virtual protected]

Handles focus out events. Repaints the current item, if set.

See also: keyPressEvent() and focusOutEvent().

Reimplemented from QWidget.

void QListBox::highlighted ( QListBoxItem * ) [signal]

This signal is emitted when the user highlights a new current item. The argument is a pointer to the new current item.

See also: selected(), currentItem() and selectionChanged().

void QListBox::highlighted ( const QString & ) [signal]

This signal is emitted when the user highlights a new current item and the new item is a string. The argument is the text of the new current item.

See also: selected(), currentItem() and selectionChanged().

void QListBox::highlighted ( int index ) [signal]

This signal is emitted when the user highlights a new current item. The argument is the index of the new item, which is already current.

See also: selected(), currentItem() and selectionChanged().

int QListBox::index ( const QListBoxItem * lbi ) const

Returns the index of lbi, or -1 if the item is not in this list box.

See also: item().

void QListBox::insertItem ( const QListBoxItem * lbi, int index=-1 )

Inserts the item lbi into the list at index.

If index is negative or larger than the number of items in the list box, lbi is inserted at the end of the list.

See also: insertStrList().

void QListBox::insertItem ( const QPixmap & pixmap, int index=-1 )

Inserts pixmap into the list at index.

If index is negative, pixmap is inserted at the end of the list.

See also: insertStrList().

void QListBox::insertItem ( const QString & text, int index=-1 )

Inserts text into the list at index.

If index is negative, text is inserted at the end of the list.

See also: insertStrList().

Examples: tabdialog/tabdialog.cpp

void QListBox::insertStrList ( const QStrList & list, int index=-1 )

Inserts the string list list into the list at item index.

If index is negative, list is inserted at the end of the list. If index is too large, the operation is ignored.

Warning: This function uses const char * rather than QString, so we recommend against using it. It is provided so that legacy code will continue to work, and so that programs that certainly will not need to handle code outside a single 8-bit locale can use it. See insertStringList() - it uses real QStrings.

Warning: This function is never significantly faster than a loop around insertItem().

See also: insertItem() and inSort().

void QListBox::insertStrList ( const QStrList * list, int index=-1 )

Inserts the string list list into the list at item index.

If index is negative, list is inserted at the end of the list. If index is too large, the operation is ignored.

Warning: This function uses const char * rather than QString, so we recommend against using it. It is provided so that legacy code will continue to work, and so that programs that certainly will not need to handle code outside a single 8-bit locale can use it. See insertStringList() - it uses real QStrings.

Warning: This function is never significantly faster than a loop around insertItem().

See also: insertItem() and inSort().

void QListBox::insertStrList ( const char ** strings, int numStrings=-1, int index=-1 )

Inserts the numStrings strings of the array strings into the list at itemindex.

If index is negative, insertStrList() inserts strings at the end of the list. If index is too large, the operation is ignored.

Warning: This function uses const char * rather than QString, so we recommend against using it. It is provided so that legacy code will continue to work, and so that programs that certainly will not need to handle code outside a single 8-bit locale can use it. See insertStringList() - it uses real QStrings.

Warning: This function is never significantly faster than a loop around insertItem().

See also: insertItem() and inSort().

void QListBox::insertStringList ( const QStringList & list, int index=-1 )

Inserts the string list list into the list at item index.

If index is negative, list is inserted at the end of the list. If index is too large, the operation is ignored.

Warning: This function is never significantly faster than a loop around insertItem().

See also: insertItem() and inSort().

bool QListBox::isMultiSelection () const

Returns TRUE if the listbox is in multi-selection mode or extended selection mode, and FALSE if it is in single-selection mode.

See also: selectionMode() and setSelectionMode().

bool QListBox::isSelected ( const QListBoxItem * i ) const

Returns TRUE if item i is selected. Returns FALSE if it is not selected or if there is an error.

bool QListBox::isSelected ( int i ) const

Returns TRUE if item i is selected. Returns FALSE if it is not selected or if there is an error.

QListBoxItem * QListBox::item ( int index ) const

Returns a pointer to the item at position index, or 0 if index is out of bounds.

See also: index().

QListBoxItem * QListBox::itemAt ( QPoint p ) const

Returns a pointer to the item at p, which is in on-screen coordinates, or a null pointer if there is no item at p.

int QListBox::itemHeight ( int index = 0 ) const

Returns the height in pixels of the item with index index. index defaults to 0.

If index is too large, this function returns 0.

QRect QListBox::itemRect ( QListBoxItem * item ) const

Returns the rectangle on the screen item occupies in viewport()'s coordinates, or an invalid rectangle if i is a null pointer or is not currently visible.

bool QListBox::itemVisible ( const QListBoxItem * item )

Returns TRUE if item is at least partly visible, or else FALSE.

bool QListBox::itemVisible ( int index )

Returns TRUE if the item at position index is at least partly visible.

void QListBox::keyPressEvent ( QKeyEvent * e ) [virtual protected]

Handles key press events.

Up and down arrow keys make the highlighted item move and if necessary scroll the list box.

Enter makes the list box emit the selected() signal.

See also: selected() and setCurrentItem().

Reimplemented from QWidget.

QSize QListBox::minimumSizeHint () const [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QListBox::mouseDoubleClickEvent ( QMouseEvent * ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QListBox::mouseMoveEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QListBox::mousePressEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QListBox::mouseReleaseEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

int QListBox::numColumns () const

Returns the number of columns in the list box. This is normally 1, but can be different if setColumnMode() or setRowMode() has been called.

See also: setColumnMode(), setRowMode() and numRows().

int QListBox::numItemsVisible () const

Returns the number of visible items. Both partially and entirely visible items are counted.

int QListBox::numRows () const

Returns the number of rows in the list box. This is equal to the number of items in the default single-column layout, but can be different.

See also: setRowMode() and numColumns().

void QListBox::paintCell ( QPainter * p, int row, int col ) [virtual protected]

Provided for compatibility with the old QListBox. We recommend using QListBoxItem::paint().

const QPixmap * QListBox::pixmap ( int index ) const

Returns a pointer to the pixmap at position index, or 0 if there is no pixmap there.

See also: text().

void QListBox::removeItem ( int index )

Removes and deletes the item at position index. If index is equal to currentItem(), a new item gets selected and the highlighted() signal is emitted.

See also: insertItem() and clear().

void QListBox::resizeEvent ( QResizeEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

QListBox::LayoutMode QListBox::rowMode() const

Returns the row layout mode for this list box. This is normally Variable, but can be changed by calling setRowMode().

See also: columnMode(), setRowMode() and numRows().

void QListBox::selected ( QListBoxItem * ) [signal]

This signal is emitted when the user double-clicks on an item or presses return when an item is highlighted. The argument is a pointer to the new selected item.

See also: highlighted() and selectionChanged().

void QListBox::selected ( const QString & ) [signal]

This signal is emitted when the user double-clicks on an item or presses return while an item is highlighted, and the selected item is (or has) a string. The argument is the text of the selected item.

See also: highlighted() and selectionChanged().

void QListBox::selected ( int index ) [signal]

This signal is emitted when the user double-clicks on an item or presses return when an item is highlighted. The argument is the index of the selected item.

See also: highlighted() and selectionChanged().

void QListBox::selectionChanged () [signal]

This signal is emitted when the selection set of a multiple-choice listbox changes. If the user selects five items by drag-selecting, QListBox tries to emit just one selectionChanged() signal, so the signal can be connected to computationally expensive slots.

See also: selected() and currentItem().

QListBox::SelectionMode QListBox::selectionMode() const

Returns the selection mode of the list box. The initial mode is Single.

See also: setSelectionMode().

void QListBox::setBottomItem ( int index ) [virtual]

Scrolls the list box so the item at position index in the list is displayed in the bottom row of the list box.

See also: setTopItem().

void QListBox::setColumnMode ( LayoutMode mode ) [virtual]

Sets the column layout mode to mode, and the number of displayed columns accordingly.

The row layout mode implicitly becomes Variable.

If mode is Variable, this function returns without doing anything.

void QListBox::setColumnMode ( int columns ) [virtual]

Sets the column layout mode for this list box to FixedNumber, and sets the number of displayed columns accordingly.

See also: setRowMode(), columnMode() and numColumns().

void QListBox::setCurrentItem ( QListBoxItem * i ) [virtual]

Sets the highlighted item to the item at position index in the list. The highlighting is moved and the list box scrolled as necessary.

See also: currentItem().

Examples: tabdialog/tabdialog.cpp

void QListBox::setCurrentItem ( int index ) [virtual]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This is a bit slower than the QListBoxItem * version.

void QListBox::setFont ( const QFont & font ) [virtual]

Reimplemented for internal reasons; the API is not affected.

void QListBox::setMultiSelection ( bool enable )

Sets the list box to multi-selection mode if enable is TRUE, and to single-selection mode if enable is FALSE. We recommend using setSelectionMode() instead; that function also offers a third mode of selection.

See also: setSelectionMode() and selectionMode().

void QListBox::setRowMode ( LayoutMode mode ) [virtual]

Sets the row layout mode to mode, and the number of displayed rows accordingly.

The column layout mode implicitly becomes Variable.

If mode is Variable, this function returns without doing anything.

void QListBox::setRowMode ( int rows ) [virtual]

Sets the row layout mode for this list box to FixedNumber and sets the number of displayed rows accordingly.

See also: setColumnMode(), rowMode() and numRows().

void QListBox::setSelected ( QListBoxItem * item, bool select ) [virtual]

Selects item if select is TRUE, or unselects it if select is FALSE, and repaints the item appropriately.

If the listbox is a single-selection listbox and and select is TRUE, setCurrentItem will be called.

If the listbox is a single-selection listbox and and select is FALSE, clearSelection() will be called if index is the currently selected item.

See also: setMultiSelection(), setCurrentItem(), clearSelection() and currentItem().

void QListBox::setSelected ( int index, bool select )

Selects the item at position index if select is TRUE, or unselects it if select is FALSE, and repaints the item appropriately.

If the listbox is a single-selection listbox and and select is TRUE, setCurrentItem will be called.

If the listbox is a single-selection listbox and and select is FALSE, clearSelection() will be called if index is the currently selected item.

See also: setMultiSelection(), setCurrentItem(), clearSelection() and currentItem().

void QListBox::setSelectionMode ( SelectionMode mode ) [virtual]

Sets the list box to selection mode mode, which may be one of Single (the default), Extended and Multi.

See also: selectionMode().

void QListBox::setTopItem ( int index ) [virtual]

Scrolls the list box so the item at position index in the list is displayed in the top row of the list box.

See also: topItem() and ensureCurrentVisible().

void QListBox::setVariableHeight ( bool enable ) [virtual]

Sets this list box to have variable-height rows if enable is TRUE, and equal-height rows if enable is FALSE.

When the list box has variable-height rows, each row is as high as the highest item in that row. When it has same-sized rows, all rows are as high as the highest item in the list box.

The default is TRUE.

See also: setVariableWidth() and variableHeight().

void QListBox::setVariableWidth ( bool enable ) [virtual]

Sets this list box to have variable-width columns if enable is TRUE, and equal-width columns if enable is FALSE.

When the list box has variable-width columns, each column is as wide as the widest item in that column. When it has same-sized columns, all columns are as wide as the widest item in the list box.

The default is FALSE.

See also: setVariableHeight() and variableWidth().

void QListBox::showEvent ( QShowEvent * ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

QSize QListBox::sizeHint () const [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QListBox::takeItem ( const QListBoxItem * item )

Removes item from the listbox and causes an update of the screen display. The item is not deleted. You should normally not need to call this function, as QListBoxItem::~QListBoxItem() calls it. The normal way to delete an item is delete.

See also: QListBox::insertItem().

QString QListBox::text ( int index ) const

Returns the text at position index, or a null string if there is no text at that position.

See also: pixmap().

void QListBox::toggleCurrentItem () [protected]

Toggles the selection status of currentItem() and repaints, if the listbox is a multi-selection listbox.

Does nothing if the listbox is a single-selection listbox.

See also: setMultiSelection().

int QListBox::topItem () const

Returns the index of an item at the top of the screen. If there are more than one of them, an arbitrary item is selected and returned.

void QListBox::triggerUpdate ( bool doLayout )

Ensures that a single paint event will occur at the end of the current event loop iteration. If doLayout is TRUE, the layout is also redone.

void QListBox::updateItem ( QListBoxItem * i ) [protected]

Repaints i.

void QListBox::updateItem ( int index ) [protected]

Repaints the item at position index in the list.

bool QListBox::variableHeight () const

Returns TRUE if this list box has variable-height rows, and FALSE if all the rows have the same height.

See also: setVariableHeight() and setVariableWidth().

bool QListBox::variableWidth () const

Returns TRUE if this list box has variable-width columns, and FALSE if all the columns have the same width.

See also: setVariableHeight() and setVariableWidth().

void QListBox::viewportMouseDoubleClickEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QScrollView.

void QListBox::viewportMouseMoveEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QScrollView.

void QListBox::viewportMousePressEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QScrollView.

void QListBox::viewportMouseReleaseEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QScrollView.

void QListBox::viewportPaintEvent ( QPaintEvent * e ) [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QScrollView.


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