1
votes

I am creating a completer myself, using ComboBox and QTreeView (for the proposal list).

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);
}

The structure of my data for the tree model here is parent-child. With the constructor above, after I put my data into the model, the children are hidden, only the parents can be seen. In order to see all the items (children) I have to use m_view->expandAll() after I put the data into the model. Is there any way that we can do it in constructor, so each time i put data into the model (whatever my data is), all the items (parents and children) are automatically expanded ?

1

1 Answers

1
votes

Your best bet is probably to connect to the QAbstractItemModel::rowsInserted signal to make sure items are expanded on a just-in-time basis. So, immediately after setting the view's model use something like...

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);
            }
        });