Core: Fix QModelIndex interaction for variable chooser

The tree has a QSortFilterProxyModel, so we have to map
the current index back to its source model to avoid
using a wrong index.

Change-Id: I78b6172a2c9b3d4255132e30e8eddf72dc95d5e4
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Stenger
2016-11-08 14:34:01 +01:00
parent ae4d3c52e6
commit 9c5e92c714

View File

@@ -118,6 +118,7 @@ public:
VariableTreeView *m_variableTree; VariableTreeView *m_variableTree;
QLabel *m_variableDescription; QLabel *m_variableDescription;
QSortFilterProxyModel *m_sortModel;
QString m_defaultDescription; QString m_defaultDescription;
QByteArray m_currentVariableName; // Prevent recursive insertion of currently expanded item QByteArray m_currentVariableName; // Prevent recursive insertion of currently expanded item
}; };
@@ -258,10 +259,10 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
m_variableTree = new VariableTreeView(q, this); m_variableTree = new VariableTreeView(q, this);
m_variableDescription = new QLabel(q); m_variableDescription = new QLabel(q);
auto sorter = new QSortFilterProxyModel(this); m_sortModel = new QSortFilterProxyModel(this);
sorter->setSourceModel(&m_model); m_sortModel->setSourceModel(&m_model);
sorter->sort(0); m_sortModel->sort(0);
m_variableTree->setModel(sorter); m_variableTree->setModel(m_sortModel);
m_variableDescription->setText(m_defaultDescription); m_variableDescription->setText(m_defaultDescription);
m_variableDescription->setMinimumSize(QSize(0, 60)); m_variableDescription->setMinimumSize(QSize(0, 60));
m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop); m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop);
@@ -416,7 +417,8 @@ void VariableChooser::addSupportForChildWidgets(QWidget *parent, MacroExpander *
void VariableChooserPrivate::updateDescription(const QModelIndex &index) void VariableChooserPrivate::updateDescription(const QModelIndex &index)
{ {
if (m_variableDescription) if (m_variableDescription)
m_variableDescription->setText(m_model.data(index, Qt::ToolTipRole).toString()); m_variableDescription->setText(m_model.data(m_sortModel->mapToSource(index),
Qt::ToolTipRole).toString());
} }
/*! /*!
@@ -534,7 +536,7 @@ QWidget *VariableChooserPrivate::currentWidget()
*/ */
void VariableChooserPrivate::handleItemActivated(const QModelIndex &index) void VariableChooserPrivate::handleItemActivated(const QModelIndex &index)
{ {
QString text = m_model.data(index, UnexpandedTextRole).toString(); QString text = m_model.data(m_sortModel->mapToSource(index), UnexpandedTextRole).toString();
if (!text.isEmpty()) if (!text.isEmpty())
insertText(text); insertText(text);
} }