Qt_Note18_QML_c++与qml信号与槽

main.qml

import QtQuick 2.14
import QtQuick.Controls 2.14
import MyObj 1.0

ApplicationWindow {
    id: window
    visible: true
    width: screen_WIDTH
    height: 480
    objectName: "window"
    title: qsTr("Scroll")

    signal qmlSig(int i,string s)   //qml端信号

    function qmlSlot(i,s){  //qml端槽函数
        console.log("qml",i,s)
    }

    Button{
        objectName: "mybutton"
        onClicked: {
//            myobj.func()  //cpp发送信号调用qml槽函数
            qmlSig(10, "lisi")    //调用cpp端槽函数
//            myobj.cppSlot(999,"wangwu")   //cpp发送信号调用qml槽函数
            MyObject.func()
        }
    }

//    MyObject{## 标题
        iValue: 10
        sString: "shangsan"

        Component.onCompleted: {
            console.log(iValue, sString)
        }
//        id: myobj
//    }

//    Connections{
        target: window  //qml发送信号调用cpp端槽函数
        target: myobj   //cpp发送信号调用qml槽函数
//        target: MyObject
        function onQmlSig(i,s){   //qml发送信号调用cpp端槽函数
            myobj.cppSlot(i,s)
        }
//        function onCppSig(i,s){     //cpp发送信号调用qml槽函数
//            qmlSlot(i,s)
//        }
//    }

//    Component.onCompleted: {
//        qmlSig.connect(myobj.cppSlot)
//    }

//    ScrollView {
//        anchors.fill: parent

//        ListView {
//            width: parent.width
//            model: 20
//            delegate: ItemDelegate {
//                text: "Item " + (index + 1)
//                width: parent.width
//            }
//        }
//    }
}

myobject.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>
#include <QtQml>

class MyObject : public QObject
{
    Q_OBJECT

//    QML_ELEMENT

public:
    explicit MyObject(QObject *parent = nullptr);
    static MyObject* getinstance();


    int iValue() const;
    void setIValue(int iValue);

    QString sString() const;
    void setSString(const QString &sString);

    Q_INVOKABLE void func();

public slots:
    void cppSlot(int i,QString s);  //接收qml端信号

signals:
    void cppSig(QVariant i, QVariant s);  //cpp_myobj向qml发送信号

private:
    int m_iValue;
    QString m_sString;

    Q_PROPERTY(int iValue READ iValue WRITE setIValue )
    Q_PROPERTY(QString sString READ sString WRITE setSString )

};

#endif // MYOBJECT_H

myobject.cpp

#include "myobject.h"

MyObject::MyObject(QObject *parent) : QObject(parent)
{

}

MyObject *MyObject::getinstance()
{
    static MyObject* obj= new MyObject();
    return obj;
}

int MyObject::iValue() const
{
    return m_iValue;
}

void MyObject::setIValue(int iValue)
{
    m_iValue = iValue;
}

QString MyObject::sString() const
{
    return m_sString;
}

void MyObject::setSString(const QString &sString)
{
    m_sString = sString;
}

void MyObject::func()
{
    emit cppSig(101,"wangwang");
    qDebug() << __FUNCTION__;

}

void MyObject::cppSlot(int i, QString s)
{
    qDebug() << __FUNCTION__ << " " << i << " " << s;
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QScreen>
#include <QRect>
#include <QQmlContext>
#include "myobject.h"
#include <QObject>

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

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    QQmlContext* context =  engine.rootContext();   //获取qml文件全局对象
    QScreen* screen = QGuiApplication::primaryScreen();

    QRect rect = screen->virtualGeometry();
    //注册的上下文对象是作用于全局的
    context->setContextProperty("screen_WIDTH",200);
//    context->setContextProperty("MyObject",MyObject::getinstance());
    qmlRegisterType<MyObject>("MyObj",1,0,"MyObject");//qmlRegisterType 是一个可以将C++实现的类在QML中调用的
    //我们一定要通过创建对象来定义一个我们自定义的myObject
    qmlRegisterSingletonInstance("MyObj",1,0,"MyObject",MyObject::getinstance());

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    //engine 加载完成后 load后
    auto list = engine.rootObjects();   //获取qml文件所有对象
    auto objName = list.first()->objectName();
    qDebug() << objName << endl;
    auto objName2 = list.first()->findChild<QObject*>("mybutton");
    qDebug() << objName2;

    auto window = list.first();
//    QObject::connect(window,SIGNAL(qmlSig(int,QString)),MyObject::getinstance(),SLOT(cppSlot(int,QString)));

    QObject::connect(MyObject::getinstance(),SIGNAL(cppSig(QVariant,QVariant)),
                     window, SLOT(qmlSlot(QVariant,QVariant)));

    return app.exec();
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可西里啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值