[英]how can I specify a c++ type in QML using qmlRegisterType?
I need use qmlRegisterType in c++ to define a commun type with QML. 我需要在c ++中使用qmlRegisterType来定义带有QML的通信类型。 That's supposed to be simple but I have an error in the QML.
这应该很简单,但我在QML中有错误。 Here's the c++.
这是c ++。
QGuiApplication app(ai_argc, aipp_argv);
qmlRegisterType<MiniModel>("miniModel", 1, 0, "MiniModel");//there it is !
QQmlApplicationEngine engine;
engine.addImportPath(QStringLiteral("..\\..\\..\\..\\Tools\\Qt\\5.12.0\\x64\\5.12.0\\msvc2017_64\\qml"));
engine.load(QUrl(QStringLiteral("../../Inc/miniModel/miniModel.qml")));
return app.exec();
and here is the QML 这是QML
import QtQuick 2.5
import QtQuick.Window 2.5
import QtQuick.controls 2.5
import "..\\..\\x64\\Debug\\" miniModel 1.0 MiniModel//unknown component
ApplicationWindow {
id: root
width: 300
height: 480
visible: true
property bool value: Minimodelqml.
BackEnd {
id: backend
}
TextField {
text: backend.userName
placeholderText: qsTr("User name")
anchors.centerIn: parent
onTextChanged: backend.userName = text
}
}
I tried : 我试过了 :
/*cpp*/qmlRegisterType<MiniModel>("miniModel", 1, 0, "MiniModel");
/*qml*/import miniModel 1.0 MiniModel //QML module not found
/*cpp*/qmlRegisterType<MiniModel>("anothertry.miniModel", 1, 0, "MiniModel");
/*cml*/import anothertry.miniModel 1.0//qml module not found
and some others... I'm new to QMl and don't know how to deal with it. 和其他一些人......我是QMl的新手,不知道如何处理它。
I'm not using a QtCreator project. 我没有使用QtCreator项目。 It's a visual studio 2017 solution.
这是一个视觉工作室2017解决方案。 Maybe the problem comes from there ?
也许问题来自那里?
edit : the code using the singleton works "fine" 编辑:使用单例的代码工作“很好”
Just import miniModel 1.0
in .qml
should work 只需在
.qml
import miniModel 1.0
.qml
/*cpp*/qmlRegisterType<MiniModel>("miniModel", 1, 0, "MiniModel");
/*qml*/import miniModel 1.0
Those are some of the frequent steps I do follow to expose C++ classes to the QML engine. 这些是我将C ++类暴露给QML引擎的一些常见步骤。
Let's start by declaring some constants: 让我们从声明一些常量开始:
struct package_manager {
static constexpr auto package_name = "com.mohabouje.package";
static constexpr auto package_version_major = 0;
static constexpr auto package_version_minor = 1;
}
Then create your Qt Object, and expose the properties to the QML engine : 然后创建您的Qt对象,并将属性公开给QML引擎 :
class MyObject : public QObject {
Q_OBJECT
Q_PROPERTY(double awesome READ awesome WRITE setAwesome NOTIFY awesomeChanged)
public:
// ...
double awesome();
void setAwesome(double);
signals:
void awesomeChanged(double);
};
In your main, then do: 在你的主要,然后做:
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
qmlRegisterType<MyObject>(package_manager::package_name,
package_manager::package_version_major,
package_manager::package_version_minor, "MyObject");
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
And your main.qml
becomes: 你的
main.qml
变成:
import com.mohabouje.pacakge 0.1
ApplicationWindow {
id: root
width: 300
height: 480
visible: true
Component.onCompleted: {
MyObject {
awesome: 25
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.