Qt 小例子学习44 - 磁贴界面
Qt 小例子学习44 - 磁贴界面
FormModel.h
#ifndef FORMMODEL_H
#define FORMMODEL_H#include <QAbstractListModel>struct Tile
{Q_GADGETQ_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_GADGETQ_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.3Window {id:appvisible: truewidth: 480height: 800title: qsTr("Hello World")ListView {model: myFormsanchors.fill: parentdelegate: ColumnLayout{spacing: 30Text{text: nameFormfont.pointSize:20color: "red"font.capitalization: Font.Capitalize}GridView {id:grwidth: 300; height: 300cellWidth: 100 ; cellHeight: 100model: grid // grid.lengthdelegate: Item{width : gr.cellWidthheight: gr.cellHeightRectangle{anchors.centerIn: parentwidth:parent.width-10height: parent.height-10color: grid[index].colorText {id: nameanchors.fill: parenttext: 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);
#endifQGuiApplication 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();
}
Qt 小例子学习44 - 磁贴界面相关推荐
- Qt小例子学习52 - 卡片游戏
Qt小例子学习52 - 卡片游戏 Card.qml import QtQuick 2.0 import QtQuick.Layouts 1.3Item {id: cardLayout.fillWidt ...
- Qt 小例子学习33 - QTableWidget 显示点击的行列
Qt 小例子学习33 - QTableWidget 显示点击的行列 #include <QApplication> #include <QDebug> #include < ...
- Qt 小例子学习26 - 画网格
Qt 小例子学习26 - 画网格 GraphicsScene.h #ifndef GRAPHICSSCENE_H #define GRAPHICSSCENE_H#include <QGraphi ...
- Qt 小例子学习8 - 选择器
Qt 小例子学习7 - 选择器 dialog.cpp #include "dialog.h" #include "ui_dialog.h"#include &l ...
- QT小例子GUI(主)线程与子线程之间的通信
QT小例子GUI(主)线程与子线程之间的通信 在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. #ifndef TQT_H_ ...
- 关于MSSQL存储过程中使用游标的一个小例子(学习)
声明:本例转自http://hi.chinaunix.net/?214950/viewspace-1713,主要是为了学习之用 比如有个表,内容如下 id,text 1,春花秋月何时了 2,往事知多少 ...
- 小例子学习C#扩展方法
扩展方法使您能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 对于用 ...
- QT小例子 ---文件查找
先看看效果图吧,可以查找文件,以及通过文件内容进行查找 #ifndef WINDOW_H #define WINDOW_H#include <QDialog> #include <Q ...
- android 仿qq修改头像,Qt:小项目仿QQ修改头像界面,技术点记录
最近写了一个修改头像功能的UI,布局参考了QQ目前的修改头像界面.如下图 这里主要说明一下两个地方的技术:1.头像图片上层的遮罩层,圆形外部为灰色,内部为全透明:2.上传图片宽高比例可以通过鼠标拖拽移 ...
最新文章
- android双重for循环,Android实现ViewPager无限循环效果(二)
- 网站推广专员浅析不做大幅修改如何调整网站推广内容?
- 用JavaScript实现简单的excel列转sql字符串
- gmod的css模块放哪里,webpack打包css报错找不到模块?
- banner轮播无缝滚动 jq代码
- 【摘录】《程序设计导引及在线实践》之排列
- 剑指Offer学习笔记(3)——解决面试题的思路
- 正确使用auto_ptr智能指针
- 西蒙决策_西蒙的象棋因子
- 使用MySQL8.0以上版本和MySQL驱动包8.0以上出现的问题
- 安装step7 v5.6报错的解决方法
- TCP/UDP测试工具大全
- 了不起的 Java VM-Java 虚拟机初探
- 中国移动和路由AP218刷机后桥接网络和配置无线打印教程
- js更新mysql数据库_更新javascript方法
- java画哆啦A梦_java绘制哆啦A梦 超可爱
- android 打开网络设置 异常,逍遥安卓模拟器显示网络异常,请查收最完整的解决方法...
- 在线客服系统|物流行业解决方案——助力企业构建物流行业新一体化模式
- Arduino与Proteus仿真实例-I2C总线多个M24C01设备数据储存仿真
- Ubuntu 22.04 LTS 是史诗级的版本?