I use QAbstractListModel
to create a custom model for ComboBox
in Qt Quick.
Code in header file:
#ifndef JREFINDER_H
#define JREFINDER_H
#include <QObject>
#include <QMap>
#include <QString>
#include <QAbstractListModel>
enum BitSize
{
BitX86,
BitX64
};
class JreInformation : public QObject
{
Q_OBJECT
public:
..........
};
class JreFinder : public QAbstractListModel
{
Q_OBJECT
public:
enum JreFinderRoles
{
JavaPath = Qt::UserRole + 1,
JavaVerson,
JavaType,
Display
};
explicit JreFinder(QObject *parent = 0);
virtual QHash<int, QByteArray> roleNames() const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
Q_INVOKABLE void refresh();
Q_INVOKABLE void setIndex(int index);
Q_INVOKABLE QObject* getJavaInfo();
signals:
public slots:
private:
int m_index;
QList<JreInformation*> m_foundJre;
QHash<int, QByteArray> m_roleNames;
};
#endif // JREFINDER_H
And the code in Source file:
JreFinder::JreFinder(QObject *parent) : QAbstractListModel(parent)
,m_index(0)
{
m_roleNames.insert(JavaPath, "javaPath");
m_roleNames.insert(JavaVerson, "javaVersion");
m_roleNames.insert(JavaType, "javaType");
m_roleNames.insert(Display, "display");
QHash<int, QByteArray> defaultRoleNames = QAbstractListModel::roleNames();
QHashIterator<int, QByteArray> i(defaultRoleNames);
while (i.hasNext())
{
i.next();
m_roleNames.insert(i.key(), i.value());
}
}
QHash<int, QByteArray> JreFinder::roleNames() const
{
return m_roleNames;
}
QVariant JreFinder::data(const QModelIndex &index, int role) const
{
qDebug()<<"row"<<index.row()<<" role"<<role<<" size"<<m_foundJre.size();
if(index.row() >= m_foundJre.size() || index.row() < 0)
return QVariant();
if(role == JavaPath)
return m_foundJre[index.row()]->path;
else if(role == JavaVerson)
return m_foundJre[index.row()]->version;
else if(role == JavaType)
return m_foundJre[index.row()]->type;
else if(role == Display || role == Qt::DisplayRole)
{
QString d = m_foundJre[index.row()]->display();
qDebug()<<"display:"<<d;
return d;
}
return QVariant();
}
int JreFinder::rowCount(const QModelIndex &) const
{
qDebug()<<m_foundJre.size();
return m_foundJre.size();
}
void JreFinder::refresh()
{
beginResetModel();
foreach (JreInformation* info, m_foundJre)
{
delete info;
}
m_foundJre.clear();
ReadJreHome(m_foundJre);
endResetModel();
}
void JreFinder::setIndex(int index)
{
m_index = index;
}
QObject* JreFinder::getJavaInfo()
{
if(m_index >= m_foundJre.size() || m_index < 0)
return NULL;
return m_foundJre[m_index];
}
And I expose it into QML using:
JreFinder jreFinder;
jreFinder.refresh();
engine.rootContext()->setContextProperty("jreFinder", &jreFinder);
And create a ComboBox using this model:
ComboBox
{
textRole: "display"
model:jreFinder
}
This code runs perfectly but look at this line in function data()
of source file:
else if(role == Display || role == Qt::DisplayRole)
I think after I set the textRole
of ComboBox
, the display role should be named "display" which is the role Display
in my custom model according to m_roleNames.insert(Display, "display");
. But it is not always correct.
Here is the qDebug()<<
output of the code above:
1
1
row 0 role 260 size 1
display: "1.8.0_51 64bit"
And
1
1
row 0 role 0 size 1
display: "1.8.0_51 64bit"
The two outputs are appeared randomly. You can find the role
argument passed into data()
function will be Zero sometimes. The Zero role means Qt::DisplayRole
in Qt.
So here is my question: If set textRole
of ComboBox
means the data role
is set equal to the value of textRole. Why should data role
sometimes becomes Qt::DisplayRole
? Is it a bug of ComboBox
?
It's not a wise idea to have ambiguous role-name to role-id mapping. The mapping must be 1:1. Qt's own DisplayRole
is already named display
. Name yours something else that is still a valid js identifier, or use Qt's as-is.