{
if (Q_UNLIKELY(!edit)) {
qWarning("QComboBox::setLineEdit: cannot set a 0 line edit");
return;
}
if (edit == d->lineEdit)
return;
edit->setText(currentText());
delete d->lineEdit;
d->lineEdit = edit;
#ifndef QT_NO_IM
qt_widget_private(d->lineEdit)->inheritsInputMethodHints = 1;
#endif
if (d->lineEdit->parent() != this)
d->lineEdit->setParent(this);
connect(d->lineEdit, SIGNAL(returnPressed()), this, SLOT(_q_returnPressed()));
connect(d->lineEdit, SIGNAL(editingFinished()), this, SLOT(_q_editingFinished()));
connect(d
->lineEdit,
SIGNAL(textChanged
(QString)),
this,
SIGNAL(editTextChanged
(QString)));
connect(d
->lineEdit,
SIGNAL(textChanged
(QString)),
this,
SIGNAL(currentTextChanged
(QString)));
// ... more
void QComboBox::setLineEdit(QLineEdit *edit)
{
Q_D(QComboBox);
if (Q_UNLIKELY(!edit)) {
qWarning("QComboBox::setLineEdit: cannot set a 0 line edit");
return;
}
if (edit == d->lineEdit)
return;
edit->setText(currentText());
delete d->lineEdit;
d->lineEdit = edit;
#ifndef QT_NO_IM
qt_widget_private(d->lineEdit)->inheritsInputMethodHints = 1;
#endif
if (d->lineEdit->parent() != this)
d->lineEdit->setParent(this);
connect(d->lineEdit, SIGNAL(returnPressed()), this, SLOT(_q_returnPressed()));
connect(d->lineEdit, SIGNAL(editingFinished()), this, SLOT(_q_editingFinished()));
connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(editTextChanged(QString)));
connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(currentTextChanged(QString)));
// ... more
To copy to clipboard, switch view to plain text mode
{
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 (!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);
}
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);
}
To copy to clipboard, switch view to plain text mode
{
if (Q_UNLIKELY(!model)) {
qWarning("QComboBox::setModel: cannot set a 0 model");
return;
}
if (model == d->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(rowsAboutToBeInserted
(QModelIndex,
int,
int)),
this, SLOT(_q_updateIndexBeforeChange()));
disconnect
(d
->model,
SIGNAL(rowsInserted
(QModelIndex,
int,
int)),
disconnect
(d
->model,
SIGNAL(rowsAboutToBeRemoved
(QModelIndex,
int,
int)),
this, SLOT(_q_updateIndexBeforeChange()));
disconnect
(d
->model,
SIGNAL(rowsRemoved
(QModelIndex,
int,
int)),
disconnect(d->model, SIGNAL(destroyed()),
this, SLOT(_q_modelDestroyed()));
disconnect(d->model, SIGNAL(modelAboutToBeReset()),
this, SLOT(_q_updateIndexBeforeChange()));
disconnect(d->model, SIGNAL(modelReset()),
this, SLOT(_q_modelReset()));
if (d->model->QObject::parent() == this)
delete d->model;
}
d->model = model;
connect(model,
SIGNAL(rowsAboutToBeInserted
(QModelIndex,
int,
int)),
this, SLOT(_q_updateIndexBeforeChange()));
connect(model,
SIGNAL(rowsInserted
(QModelIndex,
int,
int)),
connect(model,
SIGNAL(rowsAboutToBeRemoved
(QModelIndex,
int,
int)),
this, SLOT(_q_updateIndexBeforeChange()));
connect(model,
SIGNAL(rowsRemoved
(QModelIndex,
int,
int)),
connect(model, SIGNAL(destroyed()),
this, SLOT(_q_modelDestroyed()));
connect(model, SIGNAL(modelAboutToBeReset()),
this, SLOT(_q_updateIndexBeforeChange()));
connect(model, SIGNAL(modelReset()),
this, SLOT(_q_modelReset()));
if (d->container) {
d->container->itemView()->setModel(model);
connect(d->container->itemView()->selectionModel(),
this,
SLOT(_q_emitHighlighted
(QModelIndex)), Qt
::UniqueConnection);
}
bool currentReset = false;
const int rowCount = count();
for (int pos=0; pos < rowCount; pos++) {
if (d->model->index(pos, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled) {
setCurrentIndex(pos);
currentReset = true;
break;
}
}
if (!currentReset)
setCurrentIndex(-1);
d->modelChanged();
}
void QComboBox::setModel(QAbstractItemModel *model)
{
Q_D(QComboBox);
if (Q_UNLIKELY(!model)) {
qWarning("QComboBox::setModel: cannot set a 0 model");
return;
}
if (model == d->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_updateIndexBeforeChange()));
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_updateIndexBeforeChange()));
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(modelAboutToBeReset()),
this, SLOT(_q_updateIndexBeforeChange()));
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_updateIndexBeforeChange()));
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(_q_updateIndexBeforeChange()));
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(modelAboutToBeReset()),
this, SLOT(_q_updateIndexBeforeChange()));
connect(model, SIGNAL(modelReset()),
this, SLOT(_q_modelReset()));
if (d->container) {
d->container->itemView()->setModel(model);
connect(d->container->itemView()->selectionModel(),
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_emitHighlighted(QModelIndex)), Qt::UniqueConnection);
}
setRootModelIndex(QModelIndex());
bool currentReset = false;
const int rowCount = count();
for (int pos=0; pos < rowCount; pos++) {
if (d->model->index(pos, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled) {
setCurrentIndex(pos);
currentReset = true;
break;
}
}
if (!currentReset)
setCurrentIndex(-1);
d->modelChanged();
}
To copy to clipboard, switch view to plain text mode
Bookmarks