📄 q3combobox.cpp
字号:
if ( rw ) setUpLineEdit(); setBackgroundMode( Qt::PaletteButton, Qt::PaletteBase );}/*! Destroys the combobox.*/Q3ComboBox::~Q3ComboBox(){ delete d;}void Q3ComboBox::setDuplicatesEnabled( bool enable ){ d->duplicatesEnabled = enable;}bool Q3ComboBox::duplicatesEnabled() const{ return d->duplicatesEnabled;}int Q3ComboBox::count() const{ if ( d->usingListBox() ) return d->listBox()->count(); else if (d->popup()) return d->popup()->count(); else return 0;}/*! \overload Inserts the \a list of strings at position \a index in the combobox. This is only for compatibility since it does not support Unicode strings. See insertStringList().*/void Q3ComboBox::insertStrList( const Q3StrList &list, int index ){ insertStrList( &list, index );}/*! \overload Inserts the \a list of strings at position \a index in the combobox. This is only for compatibility since it does not support Unicode strings. See insertStringList().*/void Q3ComboBox::insertStrList( const Q3StrList *list, int index ){ if ( !list ) {#if defined(QT_CHECK_NULL) Q_ASSERT( list != 0 );#endif return; } Q3StrListIterator it( *list ); const char* tmp; if ( index < 0 ) index = count(); while ( (tmp=it.current()) ) { ++it; if ( d->usingListBox() ) d->listBox()->insertItem( QString::fromLatin1(tmp), index ); else d->popup()->insertItem( escapedComboString(QString::fromLatin1(tmp)), index, index ); if ( index++ == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); currentChanged(); } } if ( index != count() ) reIndex();}/*! Inserts the \a list of strings at position \a index in the combobox.*/void Q3ComboBox::insertStringList( const QStringList &list, int index ){ QStringList::ConstIterator it = list.begin(); if ( index < 0 ) index = count(); while ( it != list.end() ) { if ( d->usingListBox() ) d->listBox()->insertItem( *it, index ); else d->popup()->insertItem( escapedComboString(*it), index, index ); if ( index++ == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); currentChanged(); } ++it; } if ( index != count() ) reIndex();}/*! Inserts the array of char * \a strings at position \a index in the combobox. The \a numStrings argument is the number of strings. If \a numStrings is -1 (default), the \a strings array must be terminated with 0. Example: \code static const char* items[] = { "red", "green", "blue", 0 }; combo->insertStrList( items ); \endcode \sa insertStringList()*/void Q3ComboBox::insertStrList( const char **strings, int numStrings, int index){ if ( !strings ) {#if defined(QT_CHECK_NULL) Q_ASSERT( strings != 0 );#endif return; } if ( index < 0 ) index = count(); int i = 0; while ( (numStrings<0 && strings[i]!=0) || i<numStrings ) { if ( d->usingListBox() ) d->listBox()->insertItem( QString::fromLatin1(strings[i]), index ); else d->popup()->insertItem( escapedComboString(QString::fromLatin1(strings[i])), index, index ); i++; if ( index++ == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); currentChanged(); } } if ( index != count() ) reIndex();}/*! Inserts a text item with text \a t, at position \a index. The item will be appended if \a index is negative.*/void Q3ComboBox::insertItem( const QString &t, int index ){ int cnt = count(); if ( !checkInsertIndex( "insertItem", name(), cnt, &index ) ) return; if ( d->usingListBox() ) d->listBox()->insertItem( t, index ); else d->popup()->insertItem( escapedComboString(t), index, index ); if ( index != cnt ) reIndex(); if ( index == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); } if ( index == d->current ) currentChanged();}/*! \overload Inserts a \a pixmap item at position \a index. The item will be appended if \a index is negative.*/void Q3ComboBox::insertItem( const QPixmap &pixmap, int index ){ int cnt = count(); if ( !checkInsertIndex( "insertItem", name(), cnt, &index ) ) return; if ( d->usingListBox() ) d->listBox()->insertItem( pixmap, index ); else d->popup()->insertItem( pixmap, index, index ); if ( index != cnt ) reIndex(); if ( index == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); } if ( index == d->current ) currentChanged();}/*! \overload Inserts a \a pixmap item with additional text \a text at position \a index. The item will be appended if \a index is negative.*/void Q3ComboBox::insertItem( const QPixmap &pixmap, const QString& text, int index ){ int cnt = count(); if ( !checkInsertIndex( "insertItem", name(), cnt, &index ) ) return; if ( d->usingListBox() ) d->listBox()->insertItem( pixmap, text, index ); else d->popup()->insertItem( pixmap, escapedComboString(text), index, index ); if ( index != cnt ) reIndex(); if ( index == d->current && d->current < count() ) { if ( d->ed ) { d->ed->setText( this->text( d->current ) ); d->updateLinedGeometry(); } else update(); } if ( index == d->current ) currentChanged();}/*! Removes the item at position \a index.*/void Q3ComboBox::removeItem( int index ){ int cnt = count(); if ( !checkIndex( "removeItem", name(), cnt, index ) ) return; if ( d->usingListBox() ) { QStyleOptionComboBox opt = d->getStyleOption(); if ( style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this) && d->popup() ) d->popup()->removeItemAt( index ); d->listBox()->removeItem( index ); } else { d->popup()->removeItemAt( index ); } if ( index != cnt-1 ) reIndex(); if ( index == d->current ) { if ( d->ed ) { QString s = QString::fromLatin1(""); if (d->current < cnt - 1) s = text( d->current ); d->ed->setText( s ); d->updateLinedGeometry(); } else { if ( d->usingListBox() ) { d->current = d->listBox()->currentItem(); } else { if (d->current > count()-1 && d->current > 0) d->current--; } update(); } currentChanged(); } else { if ( !d->ed ) { if (d->current < cnt - 1) setCurrentItem( d->current ); else setCurrentItem( d->current - 1 ); } }}/*! Removes all combobox items.*/void Q3ComboBox::clear(){ QStyleOptionComboBox opt = d->getStyleOption(); if ( d->usingListBox() ) { if ( style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this) && d->popup() ) d->popup()->clear(); d->listBox()->resize( 0, 0 ); d->listBox()->clear(); } else { d->popup()->clear(); } if(d->popup() && style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) d->popup()->setItemChecked(d->current, false); d->current = 0; if ( d->ed ) { d->ed->setText( QString::fromLatin1("") ); d->updateLinedGeometry(); } currentChanged();}QString Q3ComboBox::currentText() const{ if ( d->ed ) return d->ed->text(); else if ( d->current < count() ) return text( currentItem() ); else return QString::null;}void Q3ComboBox::setCurrentText( const QString& txt ){ int i; for ( i = 0; i < count(); i++) if ( text( i ) == txt ) break; if ( i < count() ) setCurrentItem( i ); else if ( d->ed ) d->ed->setText( txt ); else changeItem( txt, currentItem() );}/*! Returns the text item at position \a index, or QString::null if the item is not a string. \sa currentText()*/QString Q3ComboBox::text( int index ) const{ if ( !checkIndex( "text", name(), count(), index ) ) return QString::null; if ( d->usingListBox() ) { return d->listBox()->text( index ); } else { QString retText = d->popup()->text(index); retText.replace("&&", "&"); return retText; }}/*! Returns the pixmap item at position \a index, or 0 if the item is not a pixmap.*/const QPixmap *Q3ComboBox::pixmap( int index ) const{ if ( !checkIndex( "pixmap", name(), count(), index ) ) return 0; if (d->usingListBox()) { return d->listBox()->pixmap( index ); } else { d->popupPixmaps[index] = d->popup()->pixmap(index); return d->popupPixmaps[index].isNull() ? 0 : &d->popupPixmaps[index]; }}/*! Replaces the item at position \a index with the text \a t.*/void Q3ComboBox::changeItem( const QString &t, int index ){ if ( !checkIndex( "changeItem", name(), count(), index ) ) return; if ( d->usingListBox() ) d->listBox()->changeItem( t, index ); else d->popup()->changeItem(index, t); if ( index == d->current ) { if ( d->ed ) { d->ed->setText( text( d->current ) ); d->updateLinedGeometry(); } else update(); }}/*! \overload Replaces the item at position \a index with the pixmap \a im, unless the combobox is editable. \sa insertItem()*/void Q3ComboBox::changeItem( const QPixmap &im, int index ){ if ( !checkIndex( "changeItem", name(), count(), index ) ) return; if ( d->usingListBox() ) d->listBox()->changeItem( im, index ); else d->popup()->changeItem(index, im); if ( index == d->current ) update();}/*! \overload Replaces the item at position \a index with the pixmap \a im and the text \a t. \sa insertItem()*/void Q3ComboBox::changeItem( const QPixmap &im, const QString &t, int index ){ if ( !checkIndex( "changeItem", name(), count(), index ) ) return; if ( d->usingListBox() ) d->listBox()->changeItem( im, t, index ); else d->popup()->changeItem(index, im, t); if ( index == d->current ) update();}int Q3ComboBox::currentItem() const{ return d->current;}void Q3ComboBox::setCurrentItem( int index ){ if ( index == d->current && !d->ed ) { return; } if ( !checkIndex( "setCurrentItem", name(), count(), index ) ) { return; } if ( d->usingListBox() && !( listBox()->item(index) && listBox()->item(index)->isSelectable() ) ) return; QStyleOptionComboBox opt = d->getStyleOption(); if(d->popup() && style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) d->popup()->setItemChecked(d->current, false); d->current = index; d->completeAt = 0; if ( d->ed ) { d->ed->setText( text( index ) ); d->updateLinedGeometry(); } // ### We want to keep ListBox's currentItem in sync, even if NOT popuped... if ( d->usingListBox() && d->listBox() ) { d->listBox()->setCurrentItem( index ); } else { internalHighlight( index ); // internalActivate( index ); ### this leads to weird behavior, as in 3.0.1 } currentChanged();}bool Q3ComboBox::autoResize() const{ return d->autoresize;}void Q3ComboBox::setAutoResize( bool enable ){ if ( (bool)d->autoresize != enable ) { d->autoresize = enable; if ( enable ) adjustSize(); }}/*! \reimp This implementation caches the size hint to avoid resizing when the contents change dynamically. To invalidate the cached value call setFont().*/QSize Q3ComboBox::sizeHint() const{ if ( isVisible() && d->sizeHint.isValid() ) return d->sizeHint; constPolish(); int i, w; QFontMetrics fm = fontMetrics(); int maxW = count() ? 18 : 7 * fm.width(QChar('x')) + 18; int maxH = QMAX( fm.lineSpacing(), 14 ) + 2; if ( !d->usingListBox() ) { w = d->popup()->sizeHint().width() - 2* d->popup()->frameWidth(); if ( w > maxW ) maxW = w; } else { for( i = 0; i < count(); i++ ) { w = d->listBox()->item( i )->width( d->listBox() ); if ( w > maxW ) maxW = w; } } QStyleOptionComboBox opt = d->getStyleOption(); d->sizeHint = (style()->sizeFromContents(QStyle::CT_ComboBox, &opt, QSize(maxW, maxH), this). expandedTo(QApplication::globalStrut())); return d->sizeHint;}/*! \internal Receives activated signals from an internal popup list and emits the activated() signal.*/void Q3ComboBox::internalActivate( int index ){ QStyleOptionComboBox opt = d->getStyleOption(); if ( d->current != index ) { if ( !d->usingListBox() || listBox()->item( index )->isSelectable() ) { if (d->popup() && style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) d->popup()->setItemChecked(d->current, false); d->current = index; currentChanged(); } } if ( d->usingListBox() ) popDownListBox(); else d->popup()->removeEventFilter( this ); d->poppedUp = false; QString t( text( index ) ); if ( d->ed ) { d->ed->setText( t ); d->updateLinedGeometry(); } emit activated( index ); emit activated( t );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -