【问题标题】:Auto expand qcombobox that is delegate in qtreeview自动展开 qtreeview 中委托的 qcombobox
【发布时间】:2021-05-07 08:30:59
【问题描述】:
我实现了自己的 QTreeModel,在第一列中我使用自定义委托,它是 QComboBox,其中包含一些字符串的自动完成。
委托是通过使用创建的
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index); 方法。
此外,仅当向某些树模型项添加新行时才会创建委托。
我的问题是,在将新项目添加到树后,是否以及如何在创建的 QComboBox 编辑器中自动扩展要选择的项目列表?
【问题讨论】:
标签:
c++
qt
delegates
qtreeview
qcombobox
【解决方案1】:
要进入编辑模式,你可以使用:
void QAbstractItemView::edit(const QModelIndex &index)
组合框将显示,但不会打开。为此,您可以覆盖 QStyledItemDelegate::setEditorData() 并在函数末尾调用 combobox->showPopup();。
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
// check correct index for combobox
QComboBox *combobox = qobject_cast<QComboBox *>(editor);
combobox->setCurrentText(index.data(Qt::EditRole).toString());
combobox->showPopup();
}