如何使用QItemSelectionModel为QComboBox
How to use QItemSelectionModel for QComboBox?
如何使用QItemSelectionModel for QComboBox?
BaseModel *baseModel = new BaseModel(data, this);
QItemSelectionModel baseModelSelected(baseModel);
ui->tableView->setModel(baseModel);
ui->comboBox->setModel(baseModel);
ui->tableView->setSelectionModel(baseModelSelected);
ui->comboBox->setSelectionModel(baseModelSelected); // can't
QComboBox
不允许您共享选择模型。但是,您可以使用视图的选择模型在用户选择列表中的新项时更新组合框。
例如:
QStringListModel* model = new QStringListModel(QStringList() << "Op1" << "Opt2" << "Opt3" << "Opt4");
QListView* view = new QListView();
view->setModel(model);
QComboBox* combobox = new QComboBox();
combobox->setMinimumWidth(200);
combobox->setModel(model);
QWidget* w = new QWidget();
QHBoxLayout* layout = new QHBoxLayout(w);
layout->addWidget(view);
layout->addWidget(combobox);
QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, [=](
QItemSelection const& newSelection, QItemSelection const& previousSelection) {
if (newSelection.isEmpty())
return; // No selected item in the view. Do nothing
// First selected item
QString const item = newSelection.indexes().first().data().toString();
combobox->setCurrentText(item);
});
相关文章:
- 在QComboBox或QListWidget中设置所选元素的样式文本?
- 如何在QGridLayout中获取QComboBox的当前文本?
- 如何更改QComboBox项目的高度大小?
- Qt 错误:QSqlQuery::value:尝试从表中检索统计信息时未定位在有效记录上 (QComboBox)
- 错误:"类 QComboBox"没有名为"setCurrentText"的成员;
- 如何获得QTreeWidgetItem中QComboBox索引更改的通知?
- 将 QList 转换为 QStringList 以插入到 QComboBox 中
- QT - 如何从 QVector<QComboBox* 获取信号"currentIndexChanged">
- 如何使用QTreeView在QComboBox中选择项目
- 点击QComboBox的下拉列表
- Qcombobox 如何更改行编辑的文本
- QCombobox,字符串旁边有项目的其他数据
- QComboBox not triggered
- 更改 QComboBox 项的光标形状
- Qcombobox :: ShowPopup()从其Qlineedit中窃取了焦点
- 在修改项目列表时,请防止可编辑的Qcombobox选择更改
- 如何使用QItemSelectionModel为QComboBox
- QComboBox 在 editTextChanged 和 currentIndexChanged 之间的关系
- 在 QComboBox 中切换复选框
- 使用QTREEVIEW中的Qcombobox委托项目