我自己创建了一个完成者,使用ComboBox和QTreeView(用于提案列表)。
MyComboBox::MyComboBox( QWidget *p_parent ) : QComboBox( p_parent )
{
setEditable(true);
m_view = new QTreeView();
m_view->expandAll(); // this command does not work!!!
m_view->setItemDelegate( new CompleterDelegate(m_view));
CompleterSourceModel *m_sourceModel = new CompleterSourceModel(this);
CompleterProxyModel *m_proxyModel = new CompleterProxyModel(this);
m_proxyModel->setSourceModel(m_sourceModel);
setView(m_view);
setModel(m_proxyModel);
connect(this, &QComboBox::currentTextChanged, this, &MyComboBox::showProposalList);
}
这里树模型的数据结构是父子。使用上面的构造函数,在将数据放入模型后,隐藏了子项,只能看到父项。为了查看所有项目(子项),我将数据放入模型后必须使用m_view->expandAll()
。有没有什么方法可以在构造函数中完成它,所以每次我将数据放入模型(无论我的数据是什么),所有项目(父项和子项)都会自动扩展?
您最好的选择可能是连接到QAbstractItemModel::rowsInserted
信号,以确保物品在即时基础上进行扩展。因此,在设置视图模型后立即使用类似......
connect(m_view->model(), &QAbstractItemModel::rowsInserted,
[this](const QModelIndex &parent, int first, int last)
{
/*
* New rows have been added to parent. If parent isn't
* already expanded then do it now.
*/
if (!m_view->isExpanded(parent)) {
m_view->expand(parent);
}
});