Qt Study
1、新建 Qobject 页面 MyObject 2、main.cpp 加入
#include "myobject.h"
qmlRegisterType<MyObject>("MyObj",1,0,"MyObject");
3、QML 中使用
import MyObj
MyObject {
}
前提 MyObject 需要注册过
- myobject.h
public:
Q_INVOKABLE void func();
- myobject.cpp
void MyObject::func()
{
qDebug() << __FUNCTION__;
}
- Main.qml
MyObject {
id: myobjid
}
Button {
onClicked: {
myobjid.func()
}
}
- myobject.h
public slots:
void cppSlot(int i, QString s);
- myobject.cpp
void MyObject::cppSlot(int i, QString s)
{
qDebug() << __FUNCTION__ << " " << i << " " << s;
}
- Main.qml
MyObject {
id: myobjid
}
Button {
onClicked: {
qmlSig(10, 'haha')
}
}
// 方式一
Connections {
target: window
function onQmlSig(i, s) {
myobjid.cppSlot(i, s)
}
}
// 方式二
Component.onCompleted: {
qmlSig.connect(myobjid.cppSlot)
}
- main.cpp
// 方式三
auto window = list.first();
QObject::connect(window, SIGNAL(qmlSig(int, QString)), MyObject::getInstance(), SLOT(cppSlot(int, QString)));
方式一
- myobject.h
signals:
void cppSig(int i, QString s);
- Main.qml
MyObject {
id: myobjid
}
Button {
onClicked: {
myobjid.cppSig(99, 'xixi')
}
}
Connections {
target: myobjid
function onCppSig(i, s) {
qmlSlot(i, s)
}
}
方式二
- myobject.h
public:
Q_INVOKABLE void func();
signals:
void cppSig(int i, QString s);
- myobject.cpp
void MyObject::func()
{
emit cppSig(100, "gogo");
}
- Main.qml
MyObject {
id: myobjid
}
Button {
onClicked: {
myobjid.func()
}
}
Connections {
target: myobjid
function onCppSig(i, s) {
qmlSlot(i, s)
}
}
// 方式三 不需要创建object对象
- main.cpp
qmlRegisterSingletonInstance("MyObj",1,0,"MyObject",MyObject::getInstance());
Button {
onClicked: {
MyObject.func()
}
}
Connections {
target: MyObject
function onCppSig(i, s) {
qmlSlot(i, s)
}
}
// 方式四
- main.cpp
QObject::connect(MyObject::getInstance(), SIGNAL(cppSig(QVariant, QVariant)), window, SLOT(qmlSlot(QVariant, QVariant)));