也许我的问题不清楚,但我希望通过代码我的问题变得更加明确。 我有一个创建这两个结构,我在FormModel类中使用它们:
struct Tile
{
QString name;
QString color;
}; Q_DECLARE_METATYPE(Tile)
struct Form
{
QString nameForm;
Tile grid[9] ; // i want for each form 4 tile that in qml are 4 rectangles
}; Q_DECLARE_METATYPE(Form)
class FormModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit FormModel(QObject *parent = 0);
~FormModel();
enum dashBoardRoles {
NameForm=Qt::UserRole+1,
Grid,
};
int rowCount(const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const;
signals:
public slots:
private:
QList<Form> dashboard;
};
这是QAbstractListModel的数据方法:
QVariant FormModel::data(const QModelIndex &index, int role) const
{
if(index.row()>0 && index.row() >= dashboard.count())
return QVariant();
Form dashTemp = dashboard[index.row()];
if(role== NameForm)
return dashTemp.nameForm;
if(role== Grid)
{
QVariant gridVect = QVariant::fromValue(dashTemp.grid);
return gridVect;
}
return QVariant();
}
QHash<int, QByteArray> FormModel::roleNames() const
{
QHash <int,QByteArray> roles;
roles [NameForm]="nameForm";
roles [Grid]="grid";
return roles;
}
这是qml代码:
Window {
id:app
visible: true
width: 480
height: 800
title: qsTr("Hello World")
ListView {
model: myForms // i have defined the name in main.cpp
anchors.fill: parent
delegate: ColumnLayout{
spacing: 30
// nome Form
Text{
text: nameForm
font.pointSize:20
color: "red"
font.capitalization: Font.Capitalize
}
// grigla mattonelle
GridView {
id:grid
width: 300; height: 300
cellWidth: 100 ; cellHeight: 100
model: 9
delegate: Item{
width : grid.cellWidth
height: grid.cellHeight
Rectangle{
anchors.centerIn: parent
width:parent.width-10
height: parent.height-10
color: grid[index].color
}
}
}
}
}
}
我如何从角色Grid的数据中获取颜色?我在qml中调用grid [index] .color ....
答案 0 :(得分:0)
QML不直接识别结构,为此您必须将Q_GADGET
与Q_PROPERTY
一起使用,建议使用QVariantList
而不是数组,因为QML可以直接转换它。以上所有我都在以下代码中实现了它:
<强> formmodel.h 强>
#ifndef FORMMODEL_H
#define FORMMODEL_H
#include <QAbstractListModel>
struct Tile
{
Q_GADGET
Q_PROPERTY(QString name MEMBER name)
Q_PROPERTY(QString color MEMBER color)
public:
QString name;
QString color;
Tile(const QString& name="", const QString& color=""){
this->name = name;
this->color = color;
}
};
Q_DECLARE_METATYPE(Tile)
struct Form
{
Q_GADGET
Q_PROPERTY(QString nameForm MEMBER nameForm)
Q_PROPERTY(QVariantList grid MEMBER grid)
public:
Form(){
}
QString nameForm;
QVariantList grid;
};
Q_DECLARE_METATYPE(Form)
class FormModel : public QAbstractListModel
{
enum dashBoardRoles {
NameForm=Qt::UserRole+1,
Grid
};
public:
FormModel(QObject *parent = Q_NULLPTR);
QHash<int, QByteArray> roleNames() const;
int rowCount(const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
private:
QList<Form> dashboard;
};
#endif // FORMMODEL_H
<强> formmodel.cpp 强>
#include "formmodel.h"
#include <QColor>
FormModel::FormModel(QObject *parent):QAbstractListModel(parent)
{
for(int i=0; i<10; i++){
Form form;
form.nameForm = QString("name %1").arg(i);
for(int j=0; j<9; j++){
QColor color(qrand() % 256, qrand() % 256, qrand() % 256);
Tile tile{QString("name %1 %2").arg(i).arg(j), color.name()};
form.grid<< QVariant::fromValue(tile);
}
dashboard<< form;
}
}
QHash<int, QByteArray> FormModel::roleNames() const
{
QHash <int,QByteArray> roles;
roles [NameForm]="nameForm";
roles [Grid]="grid";
return roles;
}
int FormModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return dashboard.count();
}
QVariant FormModel::data(const QModelIndex &index, int role) const
{
if(index.row()<0 && index.row() >= dashboard.count())
return QVariant();
Form dashTemp = dashboard[index.row()];
if(role== NameForm)
return dashTemp.nameForm;
else if(role== Grid)
return dashTemp.grid;
return QVariant();
}
<强> main.qml 强>
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
id:app
visible: true
width: 480
height: 800
title: qsTr("Hello World")
ListView {
model: myForms
anchors.fill: parent
delegate: ColumnLayout{
spacing: 30
Text{
text: nameForm
font.pointSize:20
color: "red"
font.capitalization: Font.Capitalize
}
GridView {
id:gr
width: 300; height: 300
cellWidth: 100 ; cellHeight: 100
model: grid // grid.length
delegate: Item{
width : gr.cellWidth
height: gr.cellHeight
Rectangle{
anchors.centerIn: parent
width:parent.width-10
height: parent.height-10
color: grid[index].color
Text {
id: name
anchors.fill: parent
text: grid[index].name
}
}
}
}
}
}
}
完整示例可在以下link中找到。