📄 qcombobox.cpp
字号:
/*! \property QComboBox::count \brief the number of items in the combobox*/int QComboBox::count() const{ Q_D(const QComboBox); return d->model->rowCount(rootModelIndex());}/*! \property QComboBox::maxCount \brief the maximum number of items allowed in the combobox Note: If you set the maximum number to be less then the current amount of items in the combobox, the extra items will be truncated. This also applies if you have set an external model on the combobox.*/void QComboBox::setMaxCount(int max){ Q_D(QComboBox); Q_ASSERT(max >= 0); if (max < count()) model()->removeRows(max, count() - max, rootModelIndex()); d->maxCount = max;}int QComboBox::maxCount() const{ Q_D(const QComboBox); return d->maxCount;}/*! \property QComboBox::autoCompletion \brief whether the combobox provides auto-completion for editable items \since 4.1 \sa editable*/bool QComboBox::autoCompletion() const{ Q_D(const QComboBox); return d->autoCompletion;}void QComboBox::setAutoCompletion(bool enable){ Q_D(QComboBox); d->autoCompletion = enable;}/*! \property QComboBox::autoCompletionCaseSensitivity \brief the case-sensitive property used in auto-completion \sa autoCompletion*/Qt::CaseSensitivity QComboBox::autoCompletionCaseSensitivity() const{ Q_D(const QComboBox); return d->autoCompletionCaseSensitivity;}void QComboBox::setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity){ Q_D(QComboBox); d->autoCompletionCaseSensitivity = sensitivity;}/*! \property QComboBox::duplicatesEnabled \brief whether the user can enter duplicate items into the combobox Note that it is always possible to programatically insert duplicate items into the combobox.*/bool QComboBox::duplicatesEnabled() const{ Q_D(const QComboBox); return d->duplicatesEnabled;}void QComboBox::setDuplicatesEnabled(bool enable){ Q_D(QComboBox); d->duplicatesEnabled = enable;}/*! \fn int QComboBox::findText(const QString &text, Qt::MatchFlags flags = Qt::MatchExactly|Qt::MatchCaseSensitive) const Returns the index of the item containing the given \a text; otherwise returns -1. The \a flags specify how the items in the combobox are searched.*//*! Returns the index of the item containing the given \a data for the given \a role; otherwise returns -1. The \a flags specify how the items in the combobox are searched.*/int QComboBox::findData(const QVariant &data, int role, Qt::MatchFlags flags) const{ Q_D(const QComboBox); QModelIndexList result; QModelIndex start = model()->index(0, d->modelColumn, rootModelIndex()); result = model()->match(start, role, data, 1, flags); if (result.isEmpty()) return -1; return result.first().row();}/*! \property QComboBox::insertPolicy \brief the policy used to determine where user-inserted items should appear in the combobox The default value is \l AtBottom, indicating that new items will appear at the bottom of the list of items. \sa InsertPolicy*/QComboBox::InsertPolicy QComboBox::insertPolicy() const{ Q_D(const QComboBox); return d->insertPolicy;}void QComboBox::setInsertPolicy(InsertPolicy policy){ Q_D(QComboBox); d->insertPolicy = policy;}/*! \property QComboBox::sizeAdjustPolicy \brief the policy describing how the size of the combobox changes when the content changes The default value is \l AdjustToContentsOnFirstShow. \sa SizeAdjustPolicy*/QComboBox::SizeAdjustPolicy QComboBox::sizeAdjustPolicy() const{ Q_D(const QComboBox); return d->sizeAdjustPolicy;}void QComboBox::setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy policy){ Q_D(QComboBox); if (policy == d->sizeAdjustPolicy) return; d->sizeAdjustPolicy = policy; d->sizeHint = QSize(); updateGeometry();}/*! \property QComboBox::minimumContentsLength \brief the value describing the minimum number of characters that will fit into the combobox. This value is ignored if \l AdjustToMinimumContentsLength is not set. The default value is 0. \sa sizeAdjustPolicy*/int QComboBox::minimumContentsLength() const{ Q_D(const QComboBox); return d->minimumContentsLength;}void QComboBox::setMinimumContentsLength(int characters){ Q_D(QComboBox); if (characters == d->minimumContentsLength || characters < 0) return; d->minimumContentsLength = characters; if (d->sizeAdjustPolicy == AdjustToMinimumContentsLength) { d->sizeHint = QSize(); updateGeometry(); }}/*! \property QComboBox::iconSize \brief the size of the icons shown in the combobox. Unless explicitly set this returns the default value of the current style.*/QSize QComboBox::iconSize() const{ Q_D(const QComboBox); if (d->iconSize.isValid()) return d->iconSize; int iconWidth = style()->pixelMetric(QStyle::PM_SmallIconSize); return QSize(iconWidth, iconWidth);}void QComboBox::setIconSize(const QSize &size){ Q_D(QComboBox); if (size == d->iconSize) return; view()->setIconSize(size); d->iconSize = size; d->sizeHint = QSize(); updateGeometry();}/*! \property QComboBox::editable \brief whether the combobox can be edited by the user*/bool QComboBox::isEditable() const{ Q_D(const QComboBox); return d->lineEdit != 0;}void QComboBox::setEditable(bool editable){ Q_D(QComboBox); if (isEditable() == editable) return; setAttribute(Qt::WA_InputMethodEnabled, editable); QStyleOptionComboBox opt = d->getStyleOption(); if (editable) { if (style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) { setItemDelegate(new QItemDelegate(view())); d->viewContainer()->updateScrollers(); view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); } QLineEdit *le = new QLineEdit(this); le->setLayoutDirection(Qt::LayoutDirection( style()->styleHint(QStyle::SH_ComboBox_LayoutDirection, &opt, this))); setLineEdit(le); } else { if (style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)) { setItemDelegate(new QComboMenuDelegate(view(), this)); d->viewContainer()->updateScrollers(); view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } delete d->lineEdit; d->lineEdit = 0; }}/*! Sets the line \a edit to use instead of the current line edit widget. The combo box takes ownership of the line edit.*/void QComboBox::setLineEdit(QLineEdit *edit){ Q_D(QComboBox); if (!edit) { Q_ASSERT(edit != 0); return; } if (edit == d->lineEdit) return; edit->setText(currentText()); delete d->lineEdit; d->lineEdit = edit; if (d->lineEdit->parent() != this) d->lineEdit->setParent(this); connect(d->lineEdit, SIGNAL(returnPressed()), this, SLOT(_q_returnPressed())); connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(_q_complete())); connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(editTextChanged(QString)));#ifdef QT3_SUPPORT connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));#endif d->lineEdit->setFrame(false); d->lineEdit->setContextMenuPolicy(Qt::NoContextMenu); d->lineEdit->setFocusProxy(this); setAttribute(Qt::WA_InputMethodEnabled); d->updateLineEditGeometry(); if (isVisible()) d->lineEdit->show(); update();}/*! Returns the line edit used to edit items in the combobox, or 0 if there is no line edit. Only editable combo boxes have a line edit.*/QLineEdit *QComboBox::lineEdit() const{ Q_D(const QComboBox); return d->lineEdit;}#ifndef QT_NO_VALIDATOR/*! \fn void QComboBox::setValidator(const QValidator *validator) Sets the \a validator to use instead of the current validator.*/void QComboBox::setValidator(const QValidator *v){ Q_D(QComboBox); if (d->lineEdit) d->lineEdit->setValidator(v);}/*! Returns the validator that is used to constrain text input for the combobox. \sa editable*/const QValidator *QComboBox::validator() const{ Q_D(const QComboBox); return d->lineEdit ? d->lineEdit->validator() : 0;}#endif // QT_NO_VALIDATOR/*! Returns the item delegate used by the popup list view. \sa setItemDelegate()*/QAbstractItemDelegate *QComboBox::itemDelegate() const{ return view()->itemDelegate();}/*! Sets the item \a delegate for the popup list view. The combobox takes ownership of the delegate. \sa itemDelegate()*/void QComboBox::setItemDelegate(QAbstractItemDelegate *delegate){ Q_ASSERT(delegate); delete view()->itemDelegate(); view()->setItemDelegate(delegate);}/*! Returns the model used by the combobox.*/QAbstractItemModel *QComboBox::model() const{ Q_D(const QComboBox); return d->model;}/*! Sets the model to be \a model. \a model must not be 0. If you want to clear the contents of a model, call clear(). \sa clear()*/void QComboBox::setModel(QAbstractItemModel *model){ Q_D(QComboBox); Q_ASSERT(model); if (!model) return; if (d->model) { disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(_q_rowsInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_modelReset())); if (d->model->QObject::parent() == this) delete d->model; } d->model = model; connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(_q_rowsInserted(QModelIndex,int,int))); connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); connect(model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); connect(model, SIGNAL(modelReset()), this, SLOT(_q_modelReset())); if (d->container) d->container->itemView()->setModel(model); if (count()) setCurrentIndex(0); else setCurrentIndex(-1);}/*! Returns the root model item index for the items in the combobox. \sa setRootModelIndex()*/QModelIndex QComboBox::rootModelIndex() const{ Q_D(const QComboBox); return QModelIndex(d->root);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -