QT QML重点内容记录

最近在搞qt的qml编程,发现相对于WPF这个qt可是相当的繁琐,所以把重点的内容记录一下

  1. ListModelList
  2. TableListModel
  3. 自定义Object
  4. 异步等待

1 ListModelList

mylistmodel.h 需要重写 QHash<int,QByteArray> roleNames() const override;其他的是QT自动生成的

c++
#ifndef MYLISTMODEL_H
#define MYLISTMODEL_H

#include <QAbstractListModel>


class NameObj
{
public:
    //初始化构造器
    NameObj(const  QString &name,const int &value):name_(name),value_(value){

    }
    QString name() const{

        return name_;
    }

    int  value() const{

        return value_;
    }


private:
    //私有变量
    QString name_;
    int  value_;

};

class MylistModel : public QAbstractListModel
{
    Q_OBJECT

public:
    explicit MylistModel(QObject *parent = nullptr);
    enum NameObjRoles {
       NameRole = Qt::UserRole + 1,
       ValueRole
     };

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    void addNameObj(NameObj const & p);//这是我自己增加的一个方法 可以不加

     QHash<int,QByteArray> roleNames() const override;
private:
    QList<NameObj> _data;

};

#endif // MYLISTMODEL_H

#include "mylistmodel.h"

MylistModel::MylistModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

//QVariant MylistModel::headerData(int section, Qt::Orientation orientation, int role) const
//{
//    // FIXME: Implement me!
//}

int MylistModel::rowCount(const QModelIndex &parent) const
{
    // For list models only the root node (an invalid parent) should return the list's size. For all
    // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
    if (parent.isValid())
        return 0;
    return _data.count();
    // FIXME: Implement me!
}

QVariant MylistModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= _data.count() || index.row() < 0)
        return QVariant();

    const NameObj &obj = _data[index.row()];

    if (role == NameRole)
        return obj.name();
    else if (role == ValueRole)
        return obj.value();

    return QVariant();

}
void MylistModel:: addNameObj(NameObj const & p) {
   beginInsertRows(QModelIndex(), rowCount(), rowCount());
   _data << p;
   endInsertRows();
 }


QHash<int,QByteArray> MylistModel:: roleNames() const {

QHash<int,QByteArray> roles;
roles[NameRole]="name";
roles[ValueRole]="value";
return roles;
}
main.cpp

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
            MylistModel data;
    NameObj p1("Dean", 176);
    data.addNameObj(p1);
    NameObj p2("Tgeb", 178);
     data.addNameObj(p2); 

    qmlRegisterType<MylistModel>("MyModel", 1, 1, "MyModellist");
    engine.rootContext()->setContextProperty("mymodel", &data);//直接注册 QT版本5.14
    //如果高于这个5.15的请使用qmlRegisterSingletonInstance会更好一点
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值