📄 qlistbox.cpp
字号:
for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) insertItem( new QListBoxText(*it), index++ );}/*! Inserts the string list \a list into the list at item \a index. If \a index is negative, \a list is inserted at the end of the list. If \a index is too large, the operation is ignored. \warning This function uses <code>const char *</code> 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(). \sa insertItem(), insertStringList()*/void QListBox::insertStrList( const QStrList & list, int index ){ QStrListIterator it( list ); const char* txt; if ( index < 0 ) index = count(); while ( (txt=it.current()) ) { ++it; insertItem( new QListBoxText(QString::fromLatin1(txt)), index++ ); } if ( hasFocus() && !d->current ) setCurrentItem( d->head );}/*! Inserts the \a numStrings strings of the array \a strings into the list at item\a index. If \a index is negative, insertStrList() inserts \a strings at the end of the list. If \a index is too large, the operation is ignored. \warning This function uses <code>const char *</code> 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(). \sa insertItem(), insertStringList()*/void QListBox::insertStrList( const char **strings, int numStrings, int index ){ if ( !strings ) {#if defined(CHECK_NULL) ASSERT( strings != 0 );#endif return; } if ( index < 0 ) index = count(); int i = 0; while ( (numStrings<0 && strings[i]!=0) || i<numStrings ) { insertItem( new QListBoxText( QString::fromLatin1(strings[i])), index + i ); i++; } if ( hasFocus() && !d->current ) setCurrentItem( d->head );}/*! Inserts the item \a lbi into the list at \a index. If \a index is negative or larger than the number of items in the list box, \a lbi is inserted at the end of the list. \sa insertStrList()*/void QListBox::insertItem( const QListBoxItem *lbi, int index ){#if defined ( CHECK_NULL ) ASSERT( lbi != 0 );#else if ( !lbi ) return;#endif if ( index < 0 ) index = d->count; if ( index >= d->count ) { insertItem( lbi, d->last ); return; } QListBoxItem * item = (QListBoxItem *)lbi; d->count++; d->cache = 0; item->lbox = this; if ( !d->head || index == 0 ) { item->n = d->head; item->p = 0; d->head = item; item->dirty = TRUE; if ( item->n ) item->n->p = item; } else { QListBoxItem * i = d->head; while ( i->n && index > 1 ) { i = i->n; index--; } if ( i->n ) { item->n = i->n; item->p = i; item->n->p = item; item->p->n = item; } else { i->n = item; item->p = i; item->n = 0; } } if ( hasFocus() && !d->current ) { d->current = d->head; updateItem( d->current ); emit highlighted( d->current ); emit highlighted( d->current->text() ); emit highlighted( index ); } triggerUpdate( TRUE );}/*! Inserts the item \a lbi into the list after the item \a after. If \a after is NULL, \a lbi is inserted at the beginning. \sa insertStrList()*/void QListBox::insertItem( const QListBoxItem *lbi, const QListBoxItem *after ){#if defined ( CHECK_NULL ) ASSERT( lbi != 0 );#else if ( !lbi ) return;#endif QListBoxItem * item = (QListBoxItem*)lbi; d->count++; d->cache = 0; item->lbox = this; if ( !d->head || !after ) { item->n = d->head; item->p = 0; d->head = item; item->dirty = TRUE; if ( item->n ) item->n->p = item; } else { QListBoxItem * i = (QListBoxItem*) after; if ( i ) { item->n = i->n; item->p = i; if ( item->n ) item->n->p = item; if ( item->p ) item->p->n = item; } } if ( after == d->last ) d->last = (QListBoxItem*) lbi; if ( hasFocus() && !d->current ) { d->current = d->head; updateItem( d->current ); emit highlighted( d->current ); emit highlighted( d->current->text() ); emit highlighted( index( d->current ) ); } triggerUpdate( TRUE );}/*! Inserts \a text into the list at \a index. If \a index is negative, \a text is inserted at the end of the list. \sa insertStrList()*/void QListBox::insertItem( const QString &text, int index ){ insertItem( new QListBoxText(text), index );}/*! Inserts \a pixmap into the list at \a index. If \a index is negative, \a pixmap is inserted at the end of the list. \sa insertStrList()*/void QListBox::insertItem( const QPixmap &pixmap, int index ){ insertItem( new QListBoxPixmap(pixmap), index );}/*! Inserts \a pixmap and \a text into the list at \a index. If \a index is negative, \a pixmap is inserted at the end of the list. \sa insertStrList()*/void QListBox::insertItem( const QPixmap &pixmap, const QString &text, int index ){ insertItem( new QListBoxPixmap(pixmap, text), index );}/*! Removes and deletes the item at position \a index. If \a index is equal to currentItem(), a new item gets highlighted and the highlighted() signal is emitted. \sa insertItem(), clear()*/void QListBox::removeItem( int index ){ delete item( index ); triggerUpdate( TRUE );}/*! Deletes all items in the list. \sa removeItem()*/void QListBox::clear(){ bool blocked = signalsBlocked(); blockSignals( TRUE ); d->clearing = TRUE; d->current = 0; QListBoxItem * i = d->head; d->head = 0; while ( i ) { QListBoxItem * n = i->n; i->n = i->p = 0; delete i; i = n; } d->count = 0; d->numRows = 1; d->numColumns = 1; d->currentRow = 0; d->currentColumn = 0; d->mousePressRow = -1; d->mousePressColumn = -1; d->mouseMoveRow = -1; d->mouseMoveColumn = -1; d->selectable.clear(); clearSelection(); blockSignals( blocked ); triggerUpdate( TRUE ); d->last = 0; d->clearing = FALSE;}/*! Returns the text at position \e index, or a \link QString::operator!() null string\endlink if there is no text at that position. \sa pixmap()*/QString QListBox::text( int index ) const{ QListBoxItem * i = item( index ); if ( i ) return i->text(); return QString::null;}/*! Returns a pointer to the pixmap at position \a index, or 0 if there is no pixmap there. \sa text()*/const QPixmap *QListBox::pixmap( int index ) const{ QListBoxItem * i = item( index ); if ( i ) return i->pixmap(); return 0;}/*! Replaces the item at position \a index with \a text. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void QListBox::changeItem( const QString &text, int index ){ if( index >= 0 && index < (int)count() ) changeItem( new QListBoxText(text), index );}/*! Replaces the item at position \a index with \a pixmap. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void QListBox::changeItem( const QPixmap &pixmap, int index ){ if( index >= 0 && index < (int)count() ) changeItem( new QListBoxPixmap(pixmap), index );}/*! Replaces the item at position \a index with \a pixmap and \a text. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void QListBox::changeItem( const QPixmap &pixmap, const QString &text, int index ){ if( index >= 0 && index < (int)count() ) changeItem( new QListBoxPixmap(pixmap, text), index );}/*! Replaces the item at position \a index with \a lbi. If \e index is negative or too large, changeItem() does nothing. \sa insertItem(), removeItem()*/void QListBox::changeItem( const QListBoxItem *lbi, int index ){ if ( !lbi || index < 0 || index >= (int)count() ) return; removeItem( index ); insertItem( lbi, index ); setCurrentItem( index );}/*! Returns the number of visible items. Both partially and entirely visible items are counted.*/int QListBox::numItemsVisible() const{ doLayout(); int columns = 0; int x = contentsX(); int i=0; while ( i < (int)d->columnPos.size()-1 && d->columnPos[i] < x ) i++; if ( i < (int)d->columnPos.size()-1 && d->columnPos[i] > x ) columns++; x += visibleWidth(); while ( i < (int)d->columnPos.size()-1 && d->columnPos[i] < x ) { i++; columns++; } int y = contentsY(); int rows = 0; while ( i < (int)d->rowPos.size()-1 && d->rowPos[i] < y ) i++; if ( i < (int)d->rowPos.size()-1 && d->rowPos[i] > y ) rows++; y += visibleHeight(); while ( i < (int)d->rowPos.size()-1 && d->rowPos[i] < y ) { i++; rows++; } return rows*columns;}/*! Returns the index of the current (highlighted) item of the list box, or -1 if no item has been selected. \sa topItem()*/int QListBox::currentItem() const{ if ( !d->current || !d->head ) return -1; return index( d->current );}/*! \fn QString QListBox::currentText() const Returns the text of the current item. This is equivalent to text(currentItem()).*//*! \overload This is a bit slower than the QListBoxItem * version.*/void QListBox::setCurrentItem( int index ){ setCurrentItem( item( index ) );}/*! Sets the highlighted item to the item \a i. The highlighting is moved and the list box scrolled as necessary. \sa currentItem()*/void QListBox::setCurrentItem( QListBoxItem * i ){ if ( !i || d->current == i ) return; QListBoxItem * o = d->current; d->current = i; if ( i && selectionMode() == Single ) { bool changed = FALSE; if ( o && o->s ) { changed = TRUE; o->s = FALSE; } if ( i && !i->s && d->selectionMode != NoSelection && i->isSelectable() ) { i->s = TRUE; changed = TRUE; emit selectionChanged( i ); } if ( changed ) emit selectionChanged(); } int ind = index( i ); d->currentColumn = ind / numRows(); d->currentRow = ind % numRows(); if ( o ) updateItem( o ); if ( i ) updateItem( i ); // scroll after the items are redrawn d->visibleTimer->start( 1, TRUE ); QString tmp; if ( i ) tmp = i->text(); int tmp2 = index( i ); emit highlighted( i ); if ( !tmp.isNull() ) emit highlighted( tmp ); emit highlighted( tmp2 ); emit currentChanged( i );}/*! Returns a pointer to the item at position \a index, or 0 if \aindex is out of bounds. \sa index()*/QListBoxItem *QListBox::item( int index ) const{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -