Qt QuickでQAbstractListModel
カスタムモデルを作成するために使用します。ComboBox
ヘッダー ファイルのコード:
#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
そしてソースファイルのコード:
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];
}
そして、次を使用して QML に公開します。
JreFinder jreFinder;
jreFinder.refresh();
engine.rootContext()->setContextProperty("jreFinder", &jreFinder);
そして、このモデルを使用して ComboBox を作成します。
ComboBox
{
textRole: "display"
model:jreFinder
}
このコードは完全に実行されますdata()
が、ソース ファイルの関数で次の行を見てください。
else if(role == Display || role == Qt::DisplayRole)
textRole
のを設定した後ComboBox
、表示の役割は「表示」という名前にする必要があると思います。これはDisplay
、のカスタム モデルの役割m_roleNames.insert(Display, "display");
です。しかし、それは常に正しいとは限りません。
qDebug()<<
上記のコードの出力は次のとおりです。
1
1
行 0 役割 260 サイズ 1
表示:「1.8.0_51 64bit」
と
1
1
行 0 役割 0 サイズ 1
表示: "1.8.0_51 64bit"
2 つの出力はランダムに表示されます。関数にrole
渡される引数がゼロになることがあります。ゼロの役割は、Qt で意味します。data()
Qt::DisplayRole
だからここに私の質問があります: のセットが textRole の値に等しく設定されていることtextRole
をComboBox
意味する場合。data role
なぜdata role
時々になる べきQt::DisplayRole
ですか?のバグComboBox
ですか?