Qt 小例子学习44 - 磁贴界面

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();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值