QML 调用C++ 单列模式
一,C++ 单列
class VidyoObject : public QObject { Q_OBJECT public: static VidyoObject* getInstance(QObject*parent=nullptr); static VidyoObject* m_instance;
二,main.cpp 定义
static QObject* VidyoObjectInstance(QQmlEngine*,QJSEngine*){
return VidyoObject::getInstance();
}
void mian(int argc,char* argv[]){
qmlRegisterSingletonType<VidyoObject>("VidyoObject", 1, 0, "VidyoObject", VidyoObjectInstance);
}
三,QML使用
import VidyoObject 1.0
四,非单列模式
可以把第二步 的静态函数去掉,注册时换成 qmlRegisterType