address Suggestions in a QListView

422
2
Jump to solution
10-29-2021 01:42 PM
ArtX
by
New Contributor

this is a Qt5.15 question + Runtime SDK Qt 100.12.0 and C++ code (not QML)

well...

I Try to use the address Suggestions in a QListView, populated directly from the SuggestListModel.

but I see just empty lines (without text) even though i see the coming rows in the QListView.

I also confirmed the model is working as expected, receiving the relative suggestions using the following code ...

    QList<Esri::ArcGISRuntime::SuggestResult> lst = m_suggestListModel->suggestResults();
qDebug() << lst.at(0).label();
qDebug() << lst.at(1).label();
...
// indeed i see the comming suggestions

this is the code i use:

SuggestParameters suggestParams;
suggestParams.setCategories(QStringList {"Address"});
suggestParams.setMaxResults(5);
m_locatorTask->suggestions()->setSuggestParameters(suggestParams);
m_suggestListModel = m_locatorTask->suggestions();
m_listview->setModel( m_suggestListModel );
m_suggestListModel->setSearchText(text);
 

any clue ?

thank you!

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi Art,

SuggestListModel doesn't automatically populate the DisplayRole, which is the default role for rendering text in a QListView: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

You can write either a custom delegate to render for custom roles, or write a proxy model that can massage one of the SuggestListModel's custom roles into a DisplayRole. Here's an example in the Qt tutorials where a similar approach is taken with the DecorationRole: https://github.com/Esri/arcgis-runtime-samples-qt/blob/main/ArcGISRuntimeSDKQt_CppSamples_Widgets/Di...

 

 

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi Art,

SuggestListModel doesn't automatically populate the DisplayRole, which is the default role for rendering text in a QListView: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

You can write either a custom delegate to render for custom roles, or write a proxy model that can massage one of the SuggestListModel's custom roles into a DisplayRole. Here's an example in the Qt tutorials where a similar approach is taken with the DecorationRole: https://github.com/Esri/arcgis-runtime-samples-qt/blob/main/ArcGISRuntimeSDKQt_CppSamples_Widgets/Di...

 

 

ArtX
by
New Contributor

oh! thank you for the explanation.
it really explains the why.
I already did another approach
subclassing the QAbstractListModel for using it with a QListView;
i'm not sure if this is the fastest way. (maybe not)


m_suggest = new QListView;
m_suggest->setModel(&m_ListModel);
connect(m_suggestListModel, &SuggestListModel::suggestCompleted, this, &ArcGisMap::suggestResults);


void ArcGisMap::suggestResults()
{
   m_ListModel.list = m_suggestListModel->suggestResults(); /// (copy)
   m_ListModel.refresh();
}


class StringListModel : public QAbstractListModel
{
Q_OBJECT

public:
   StringListModel(QObject *parent = nullptr)
   : QAbstractListModel(parent) {}

int rowCount(const QModelIndex &parent) const override
{
   return list.count();
}

QVariant data(const QModelIndex &index, int role) const override
{
   if (!index.isValid())
   return QVariant();

   if (index.row() >= list.size())
   return QVariant();

   if (role == Qt::DisplayRole)
      return list.at(index.row()).label();
   else
      return QVariant();
}

void refresh()
{
   emit dataChanged(index(0), index(0));
}

QList<Esri::ArcGISRuntime::SuggestResult> list;
};

0 Kudos