📄 q3combobox.cpp
字号:
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(QLatin1String("&&"), QString(QLatin1Char('&'))); 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();}/*! Returns true if auto-resize is enabled; otherwise returns false. \sa autoResize*/bool Q3ComboBox::autoResize() const{ return d->autoresize;}/*! If \a enable is true, enable auto-resize; disable it otherwise. \sa 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(QLatin1Char('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 );}/*! \internal Receives highlighted signals from an internal popup list and emits the highlighted() signal.*/void Q3ComboBox::internalHighlight( int index ){ emit highlighted( index ); QString t = text( index ); if ( !t.isNull() ) emit highlighted( t );}/*! \internal Receives timeouts after a click. Used to decide if a Motif style popup should stay up or not after a click.*/void Q3ComboBox::internalClickTimeout(){ d->shortClick = false;}/*! Sets the palette for both the combobox button and the combobox popup list to \a palette.*/void Q3ComboBox::setPalette( const QPalette &palette ){ QWidget::setPalette( palette ); if ( d->listBox() ) d->listBox()->setPalette( palette ); if ( d->popup() ) d->popup()->setPalette( palette );}/*! Sets the font for both the combobox button and the combobox popup list to \a font.*/void Q3ComboBox::setFont( const QFont &font ){ d->sizeHint = QSize(); // invalidate size hint QWidget::setFont( font ); if ( d->usingListBox() ) d->listBox()->setFont( font ); else d->popup()->setFont( font ); if (d->ed) d->ed->setFont( font ); if ( d->autoresize ) adjustSize();}/*!\reimp*/void Q3ComboBox::resizeEvent( QResizeEvent * e ){ if ( d->ed ) d->updateLinedGeometry(); if ( d->listBox() ) d->listBox()->resize( width(), d->listBox()->height() ); QWidget::resizeEvent( e );}/*!\reimp*/void Q3ComboBox::paintEvent( QPaintEvent * ){ QPainter p( this ); const QColorGroup & g = colorGroup(); p.setPen(g.text()); if ( width() < 5 || height() < 5 ) { qDrawShadePanel( &p, rect(), g, false, 2, &g.brush( QColorGroup::Button ) ); return; } QStyleOptionComboBox opt = d->getStyleOption(); bool reverse = QApplication::reverseLayout(); if ( !d->usingListBox() && style()->styleHint(QStyle::SH_GUIStyle) == Qt::MotifStyle) { // motif 1.x style int dist, buttonH, buttonW; dist = 8; buttonH = 7; buttonW = 11; int xPos; int x0; int w = width() - dist - buttonW - 1; if ( reverse ) { xPos = dist + 1; x0 = xPos + 4; } else { xPos = w; x0 = 4; } qDrawShadePanel( &p, rect(), g, false, style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this), &g.brush( QColorGroup::Button ) ); qDrawShadePanel( &p, xPos, (height() - buttonH)/2, buttonW, buttonH, g, false, style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this) ); QRect clip( x0, 2, w - 2 - 4 - 5, height() - 4 ); QString str = d->popup()->text( this->d->current ); if ( !str.isNull() ) { p.drawText( clip, Qt::AlignCenter | Qt::TextSingleLine, str ); } QPixmap pix = d->popup()->pixmap( this->d->current ); QIcon iconSet = d->popup()->iconSet( this->d->current ); if (!pix.isNull() || !iconSet.isNull()) { QPixmap pm = ( !pix.isNull() ? pix : iconSet.pixmap() ); p.setClipRect( clip ); p.drawPixmap( 4, (height()-pm.height())/2, pm ); p.setClipping( false ); } if ( hasFocus() ) p.drawRect( xPos - 5, 4, width() - xPos + 1 , height() - 8 ); } else if(!d->usingListBox()) { style()->drawComplexControl(QStyle::CC_ComboBox, &opt, &p, this); QRect re = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this); p.setClipRect( re ); QString str = d->popup()->text( this->d->current ); QPixmap pix = d->popup()->pixmap( this->d->current ); if ( !str.isNull() ) { p.save(); p.setFont(font()); QFontMetrics fm(font()); int x = re.x(), y = re.y() + fm.ascent(); x += pix.width() + 5; p.drawText( x, y, str ); p.restore(); } if (!pix.isNull()) { p.fillRect(re.x(), re.y(), pix.width() + 4, re.height(), colorGroup().brush(QColorGroup::Base)); p.drawPixmap(re.x() + 2, re.y() + (re.height() - pix.height()) / 2, pix); } } else { style()->drawComplexControl(QStyle::CC_ComboBox, &opt, &p, this); QRect re = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this); p.setClipRect(re); if ( !d->ed ) { Q3ListBoxItem * item = d->listBox()->item( d->current ); if ( item ) { int itemh = item->height( d->listBox() ); p.translate( re.x(), re.y() + (re.height() - itemh)/2 ); item->paint( &p ); } } else if ( d->listBox() && d->listBox()->item( d->current ) ) { Q3ListBoxItem * item = d->listBox()->item( d->current ); const QPixmap *pix = item->pixmap(); if ( pix ) { p.fillRect( re.x(), re.y(), pix->width() + 4, re.height(), colorGroup().brush( QColorGroup::Base ) ); p.drawPixmap( re.x() + 2, re.y() + ( re.height() - pix->height() ) / 2, *pix ); } } p.setClipping( false ); }}/*!\reimp*/void Q3ComboBox::mousePressEvent( QMouseEvent *e ){ if ( e->button() != Qt::LeftButton ) return; if ( d->discardNextMousePress ) { d->discardNextMousePress = false; return; } QStyleOptionComboBox opt = d->getStyleOption(); QRect arrowRect = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow , this); // Correction for motif style, where arrow is smaller // and thus has a rect that doesn't fit the button. arrowRect.setHeight( QMAX( height() - (2 * arrowRect.y()), arrowRect.height() ) ); if ( count() && ( !editable() || arrowRect.contains( e->pos() ) ) ) { d->arrowPressed = false; if ( d->usingListBox() ) { listBox()->blockSignals( true ); qApp->sendEvent( listBox(), e ); // trigger the listbox's autoscroll listBox()->setCurrentItem(d->current); listBox()->blockSignals( false ); popup(); if ( arrowRect.contains( e->pos() ) ) { d->arrowPressed = true; d->arrowDown = true; repaint( false ); } } else { popup(); } QTimer::singleShot( 200, this, SLOT(internalClickTimeout())); d->shortClick = true; }}/*!\reimp*/void Q3ComboBox::mouseMoveEvent( QMouseEvent * )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -