📄 q3combobox.cpp
字号:
static inline bool checkInsertIndex( const char *method, const char * name, int count, int *index){ bool range_err = (*index > count);#if defined(QT_CHECK_RANGE) if ( range_err ) qWarning( "Q3ComboBox::%s: (%s) Index %d out of range", method, name ? name : "<no name>", *index );#else Q_UNUSED( method ) Q_UNUSED( name )#endif if ( *index < 0 ) // append *index = count; return !range_err;}static inline bool checkIndex( const char *method, const char * name, int count, int index ){ bool range_err = (index >= count);#if defined(QT_CHECK_RANGE) if ( range_err ) qWarning( "Q3ComboBox::%s: (%s) Index %i out of range", method, name ? name : "<no name>", index );#else Q_UNUSED( method ) Q_UNUSED( name )#endif return !range_err;}/*! Constructs a combobox widget with parent \a parent called \a name. This constructor creates a popup list if the program uses Motif (or Aqua) look and feel; this is compatible with Motif 1.x and Aqua. Note: If you use this constructor to create your Q3ComboBox, then the pixmap() function will always return 0. To workaround this, use the other constructor.*/Q3ComboBox::Q3ComboBox( QWidget *parent, const char *name ) : QWidget( parent, name, Qt::WNoAutoErase ){ d = new Q3ComboBoxData( this ); QStyleOptionComboBox opt; opt.init(this); if ( style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this) || style()->styleHint(QStyle::SH_GUIStyle, &opt, this) == Qt::MotifStyle ) { d->setPopupMenu( new Q3ComboBoxPopup( this, "in-combo" ) ); d->popup()->setFont( font() ); connect( d->popup(), SIGNAL(activated(int)), SLOT(internalActivate(int)) ); connect( d->popup(), SIGNAL(highlighted(int)), SLOT(internalHighlight(int)) ); } else { setUpListBox(); } d->ed = 0; d->current = 0; d->maxCount = INT_MAX; d->sizeLimit = 10; d->p = AtBottom; d->autoresize = false; d->poppedUp = false; d->arrowDown = false; d->arrowPressed = false; d->discardNextMousePress = false; d->shortClick = false; d->useCompletion = false; d->completeAt = 0; d->completeNow = false; d->completionTimer = new QTimer( this ); setFocusPolicy( Qt::TabFocus ); setBackgroundMode( Qt::PaletteButton );}/*! Constructs a combobox with a maximum size and either Motif 2.0 or Windows look and feel. The input field can be edited if \a rw is true, otherwise the user may only choose one of the items in the combobox. The \a parent and \a name arguments are passed on to the QWidget constructor.*/Q3ComboBox::Q3ComboBox( bool rw, QWidget *parent, const char *name ) : QWidget( parent, name, Qt::WNoAutoErase ){ d = new Q3ComboBoxData( this ); setUpListBox(); QStyleOptionComboBox opt = d->getStyleOption(); if(d->popup() && style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) d->popup()->setItemChecked(d->current, false); d->maxCount = INT_MAX; setSizeLimit(10); d->p = AtBottom; d->autoresize = false; d->poppedUp = false; d->arrowDown = false; d->discardNextMousePress = false; d->shortClick = false; d->useCompletion = false; d->completeAt = 0; d->completeNow = false; d->completionTimer = new QTimer( this ); setFocusPolicy( Qt::StrongFocus ); d->ed = 0; 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -