⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qcombobox.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    \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.    \warning You should not share the same instance of a delegate between comboboxes,    widget mappers or views. Doing so can cause incorrect or unintuitive editing behavior    since each view connected to a given delegate may receive the    \l{QAbstractItemDelegate::}{closeEditor()} signal, and attempt to access, modify or    close an editor that has already been closed.    \sa itemDelegate()*/void QComboBox::setItemDelegate(QAbstractItemDelegate *delegate){    if (!delegate) {        qWarning("QComboBox::setItemDelegate: cannot set a 0 delegate");        return;    }    delete view()->itemDelegate();    view()->setItemDelegate(delegate);}/*!    Returns the model used by the combobox.*/QAbstractItemModel *QComboBox::model() const{    Q_D(const QComboBox);    if (d->model == QAbstractItemModelPrivate::staticEmptyModel()) {        QComboBox *that = const_cast<QComboBox*>(this);        that->setModel(new QStandardItemModel(0, 1, that));    }    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);    if (!model) {        qWarning("QComboBox::setModel: cannot set a 0 model");        return;    }#ifndef QT_NO_COMPLETER    if (d->lineEdit && d->lineEdit->completer()        && d->lineEdit->completer() == d->completer)        d->lineEdit->completer()->setModel(model);#endif    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()) {        for (int pos=0; pos < count(); pos++) {            if (d->model->index(pos, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled) {                setCurrentIndex(pos);                break;            }        }    } 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);}/*!    Sets the root model item \a index for the items in the combobox.    \sa rootModelIndex()*/void QComboBox::setRootModelIndex(const QModelIndex &index){    Q_D(QComboBox);    d->root = QPersistentModelIndex(index);    view()->setRootIndex(index);    update();}/*!    \property QComboBox::currentIndex    \brief the index of the current item in the combobox. The index    can change when inserting or removing items. Returns -1 if no    current item is set or the combobox is empty.*/int QComboBox::currentIndex() const{    Q_D(const QComboBox);    return d->currentIndex.row();}void QComboBox::setCurrentIndex(int index){    Q_D(QComboBox);    QModelIndex mi = d->model->index(index, d->modelColumn, d->root);    bool indexChanged = (mi != d->currentIndex);    if (indexChanged)        d->currentIndex = QPersistentModelIndex(mi);    if (d->lineEdit && d->lineEdit->text() != itemText(d->currentIndex.row())) {        d->lineEdit->setText(itemText(d->currentIndex.row()));        d->updateLineEditGeometry();    }    if (indexChanged) {        update();        d->_q_emitCurrentIndexChanged(d->currentIndex);    }}/*!    \property QComboBox::currentText    \brief the text of the current item*/QString QComboBox::currentText() const{    Q_D(const QComboBox);    if (d->lineEdit)        return d->lineEdit->text();    else if (d->currentIndex.isValid())	return d->itemText(d->currentIndex);    else	return QString();}/*!    Returns the text for the given \a index in the combobox.*/QString QComboBox::itemText(int index) const{    Q_D(const QComboBox);    QModelIndex mi = d->model->index(index, d->modelColumn, d->root);    return d->itemText(mi);}/*!    Returns the icon for the given \a index in the combobox.*/QIcon QComboBox::itemIcon(int index) const{    Q_D(const QComboBox);    QVariant decoration = d->model->data(d->model->index(index, d->modelColumn, d->root), Qt::DecorationRole);    if (decoration.type() == QVariant::Pixmap)        return QIcon(qvariant_cast<QPixmap>(decoration));    else        return qvariant_cast<QIcon>(decoration);}/*!   Returns the data for the given \a role in the given \a index in the   combobox, or QVariant::Invalid if there is no data for this role.*/QVariant QComboBox::itemData(int index, int role) const{    Q_D(const QComboBox);    QModelIndex mi = d->model->index(index, d->modelColumn, d->root);    return d->model->data(mi, role);}/*!  \fn void QComboBox::insertItem(int index, const QString &text, const QVariant &userData)  Inserts the \a text and \a userData into the combobox at the given \a index.  If the index is equal to or higher than the total number of items, the new item  is appended to the list of existing items. If the index is zero or negative, the  new item is prepended to the list of existing items.  \sa insertItems()*//*!    Inserts the \a icon, \a text and \a userData into the combobox at the given \a index.    If the index is equal to or higher than the total number of items, the new item    is appended to the list of existing items. If the index is zero or negative, the    new item is prepended to the list of existing items.    \sa insertItems()*/void QComboBox::insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userData){    Q_D(QComboBox);    int itemCount = count();    index = qBound(0, index, itemCount);    if (index >= d->maxCount)        return;    // For the common case where we are using the built in QStandardItemModel    // construct a QStandardItem, reducing the number of expensive signals from the model    if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(d->model)) {        QStandardItem *item = new QStandardItem(text);        if (!icon.isNull()) item->setData(icon, Qt::DecorationRole);        if (userData.isValid()) item->setData(userData, Qt::UserRole);        m->insertRow(index, item);        ++itemCount;    } else {        d->inserting = true;        if (d->model->insertRows(index, 1, d->root)) {            QModelIndex item = d->model->index(index, d->modelColumn, d->root);            if (icon.isNull() && !userData.isValid()) {                d->model->setData(item, text, Qt::EditRole);            } else {                QMap<int, QVariant> values;                if (!text.isNull()) values.insert(Qt::EditRole, text);                if (!icon.isNull()) values.insert(Qt::DecorationRole, icon);                if (userData.isValid()) values.insert(Qt::UserRole, userData);                if (!values.isEmpty()) d->model->setItemData(item, values);            }            d->inserting = false;            d->_q_rowsInserted(d->root, index, index);            ++itemCount;        } else {            d->inserting = false;        }    }    if (itemCount > d->maxCount)        d->model->removeRows(itemCount - 1, itemCount - d->maxCount, d->root);}/*!    Inserts the strings from the \a list into the combobox as separate items,    starting at the \a index specified.    If the index is equal to or higher than the total number of items, the new items    are appended to the list of existing items. If the index is zero or negative, the    new items are prepended to the list of existing items.    \sa insertItem()    */void QComboBox::insertItems(int index, const QStringList &list){    Q_D(QComboBox);    if (list.isEmpty())        return;    index = qBound(0, index, count());    int insertCount = qMin(d->maxCount - index, list.count());    if (insertCount <= 0)        return;    // For the common case where we are using the built in QStandardItemModel    // construct a QStandardItem, reducing the number of expensive signals from the model    if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(d->model)) {        QList<QStandardItem *> items;        QStandardItem *hiddenRoot = m->invisibleRootItem();        for (int i = 0; i < insertCount; ++i)            items.append(new QStandardItem(list.at(i)));        hiddenRoot->insertRows(index, items);    } else {        d->inserting = true;        if (d->model->insertRows(index, insertCount, d->root)) {            QModelIndex item;            for (int i = 0; i < insertCount; ++i) {                item = d->model->index(i+index, d->modelColumn, d->root);                d->model->setData(item, list.at(i), Qt::EditRole);            }            d->inserting = false;            d->_q_rowsInserted(d->root, index, index + insertCount - 1);        } else {            d->inserting = false;        }    }    int mc = count();    if (mc > d->maxCount)        d->model->removeRows(d->maxCount, mc - d->maxCount, d->root);}/*!    Removes the item at the given \a index from the combobox.    This will update the current index if the index is removed.*/void QComboBox::removeItem(int index){    Q_ASSERT(index >= 0 && index < count());    Q_D(QComboBox);    d->model->removeRows(index, 1, d->root);}/*!    Sets the \a text for the item on the given \a index in the combobox.*/void QComboBox::setItemText(int index, const QString &text){    Q_D(const QComboBox);    QModelIndex item = d->model->index(index, d->modelColumn, d->root);    if (item.isValid()) {        d->model->setData(item, text, Qt::EditRole);    }}/*!    Sets the \a icon for the item on the given \a index in the combobox.*/void QComboBox::setItemIcon(int index, const QIcon &icon){    Q_D(const QComboBox);    QModelIndex item = d->model->index(index, d->modelColumn, d->root);    if (item.isValid()) {        d->model->setData(item, icon, Qt::DecorationRole);    }}/*!    Sets the data \a role for the item on the given \a index in the combobox    to the specified \a value.*/void QComboBox::setItemData(int index, const QVariant &value, int role){    Q_D(const QComboBox);    QModelIndex item = d->model->index(index, d->modelColumn, d->root);    if (item.isValid()) {        d->model->setData(item, value, role);    }}/*!    Returns the list view used for the combobox popup.*/QAbstractItemView *QComboBox::view() const{    Q_D(const QComboBox);    return const_cast<QComboBoxPrivate*>(d)->viewContainer()->itemView();}/*!  Sets the view to be used in the combobox popup to the given \a  itemView. The combobox takes ownership of the view.  Note: If you want to use the convenience views (like QListWidget,  QTableWidget or QTreeWidget), make sure to call setModel() on the  combobox with the convenience widgets model before calling this  function.*/void QComboBox::setView(QAbstractItemView *itemView){    Q_D(QComboBox);    if (!itemView) {        qWarning("QComboBox::setView: cannot set a 0 view");        return;    }    if (itemView->model() != d->model)        itemView->setModel(d->model);    d->viewContainer()->setItemView(itemView);}/*!    \reimp*/QSize QComboBox::minimumSizeHint() const{    Q_D(const QComboBox);    return d->recomputeSizeHint(d->minimumSizeHint);}/*!    \reimp    This implementation caches the size hint to avoid resizing when    the contents change dynamically. To invalidate the cached value    change the \l sizeAdjustPolicy.*/QSize QComboBox:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -