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();
}
本文含有隐藏内容,请 开通VIP 后查看