找回密码
 立即注册
收起左侧

自定义的listmodel要传给qml只能通过main函数中的engine或view么?

2
回复
4295
查看
[复制链接]
累计签到:2 天
连续签到:1 天
来源: 2018-12-21 11:40:35 显示全部楼层 |阅读模式
1Qter豆
本帖最后由 西洛 于 2018-12-21 11:40 编辑

main函数:
QList<QObject*> dataList;
    for(int i=0;i<20;i++){
        dataList.append(new DataObject("Item"+QString::number(i+1), QString::number(i+1)));
    }

    engine.rootContext()->setContextProperty
            ("myModel", QVariant::fromValue(dataList));


qml可以使用myModel

但是在其他函数中使用engine传递model,却提示没有定义model呢
QList<QObject*> dataList;
    dataList.append(new DataObject("赵大","11"));
    dataList.append(new DataObject("钱二","22"));
    dataList.append(new DataObject("孙三","33"));
    dataList.append(new DataObject("李四","44"));
    dataList.append(new DataObject("周五","55"));

    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty("other_Model",QVariant::fromValue(dataList));

回复

使用道具 举报

累计签到:742 天
连续签到:1 天
2018-12-21 18:58:13 显示全部楼层
你还是上代码吧,别的函数是哪儿的函数,你传的 engine 是哪一个 engine , QMl 的上下文只属于 main 函数里面的那个 engine,其他的engine变量其实没有用的
回复

使用道具 举报

累计签到:2 天
连续签到:1 天
2018-12-21 22:42:40 显示全部楼层
本帖最后由 西洛 于 2018-12-21 23:05 编辑

我老师要我做一个连接蓝牙设备的程序,能检测到蓝牙联系的的信息,但是设备信息不能传到qml中,main函数中的数据可以传过去:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>

#include "datamodel.h"
#include "bluetoothconnect.h"


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    BluetoothConnect btc;

    btc.startScan();

    QList<QObject*> dataList;
    for(int i=0;i<20;i++){
        dataList.append(new DataObject("Item"+QString::number(i+1), QString::number(i+1)));
    }

    qmlRegisterType<BluetoothConnect>("io.BluetoothConnect",1,0,"BluetoothConnect");

    engine.rootContext()->setContextProperty
            ("myModel", QVariant::fromValue(dataList));
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}




#include "bluetoothconnect.h"
#include "datamodel.h"

#include <QDebug>
#include <QBluetoothUuid>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>

static QString devicename;
static QString deviceaId ;

BluetoothConnect::BluetoothConnect():localDevice(new QBluetoothLocalDevice)
{
    deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent();
    connect(deviceDiscoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this, SLOT(addDevice(QBluetoothDeviceInfo)));


}

BluetoothConnect::~BluetoothConnect()
{

}

void BluetoothConnect::addDevice(const QBluetoothDeviceInfo &info)
{

    devicename = QString("%1").arg(info.name());
    deviceId =QString("%1").arg(info.address().toString());
    qDebug()<<"连接"<<devicename<<endl;
    qDebug()<<"连接"<<deviceaId<<endl;
    QList<QObject*> dataList;
    dataList.append(new DataObject(devicename,deviceId);
    engine.rootContext()->setContextProperty("myModel",QVariant::fromValue(btc.getName()))


}

void BluetoothConnect::startScan()
{
    deviceDiscoveryAgent->start();
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了