Qt 小例子学习44 - 磁贴界面
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
}
}
}
}
}
}
}
main.cpp
#include "formmodel.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
FormModel model;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myForms", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}