C++ 从QML读取值,单位为C++;
我试图从C++ 从QML读取值,单位为C++;,c++,qt,qml,C++,Qt,Qml,我试图从c++上的QML侧读取int值,但该值保持0并且在c++侧不更新,而console.log()清楚地告诉我,它在每次单击后都会增加。我做错了什么 .qml文件 FocusScope { property alias myrecordint: startbutton.recordint Rectangle { id: buttonPaneShadow width: bottomColumn.width +
c++
上的QML
侧读取int值,但该值保持0
并且在c++
侧不更新,而console.log()
清楚地告诉我,它在每次单击后都会增加。我做错了什么
.qml文件
FocusScope {
property alias myrecordint: startbutton.recordint
Rectangle {
id: buttonPaneShadow
width: bottomColumn.width + 16
height: parent.height
anchors.top: parent.top
anchors.right: parent.right
color: "white"
Column {
anchors {
right: parent.right
top: parent.top
margins: 8
}
id: buttonsColumn
anchors.rightMargin: 20
anchors.topMargin: 20
spacing: 12
CameraButton {
id: startbutton
property int recordint:0
text: "Record"
visible: camera.videoRecorder.recorderStatus == CameraRecorder.LoadedStatus
onClicked:
{
recordint=recordint+1
camera.videoRecorder.record()
console.log(recordint)
}
}
}
}
}
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
QObject *object = component.create();
qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
.cpp文件
FocusScope {
property alias myrecordint: startbutton.recordint
Rectangle {
id: buttonPaneShadow
width: bottomColumn.width + 16
height: parent.height
anchors.top: parent.top
anchors.right: parent.right
color: "white"
Column {
anchors {
right: parent.right
top: parent.top
margins: 8
}
id: buttonsColumn
anchors.rightMargin: 20
anchors.topMargin: 20
spacing: 12
CameraButton {
id: startbutton
property int recordint:0
text: "Record"
visible: camera.videoRecorder.recorderStatus == CameraRecorder.LoadedStatus
onClicked:
{
recordint=recordint+1
camera.videoRecorder.record()
console.log(recordint)
}
}
}
}
}
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
QObject *object = component.create();
qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
QQmlEngine发动机;
QQmlComponent组件(&engine,QUrl(“qrc:/VideoCaptureControls.qml”);
QObject*object=component.create();
qDebug()编辑
在前后注释之后,似乎您没有使用您应该使用的实例,因为您是通过单击按钮创建一个新实例的
此外,属性的读取只执行一次,不会自动更新
这是一个经典,你应该努力使用一个C++模型,正如Arnes(和)所指出的。
原始答案(错误,因为属性是顶级的)
只能从QML对象的顶层读取属性(从对象外部读取时)。您应该按照以下建议移动属性:
Rectangle {
id: buttonPaneShadow
width: bottomColumn.width + 16
height: parent.height
anchors.top: parent.top
anchors.right: parent.right
color: "white"
property int recordint:0 //move to here
Column {
anchors {
right: parent.right
top: parent.top
margins: 8
}
id: buttonsColumn
anchors.rightMargin: 20
anchors.topMargin: 20
spacing: 12
CameraButton {
id: startbutton
text: "Record"
visible: camera.videoRecorder.recorderStatus == CameraRecorder.LoadedStatus
onClicked:
{
recordint=recordint+1
camera.videoRecorder.record()
console.log(recordint)
}
}
}
}
就我个人而言,我不喜欢你实施这一点的方式。我更喜欢使用具有更新属性的C++模型,但这取决于您的,这是一个非常糟糕的方式,将C++暴露为QML,反之亦然。QT已经提供了一种连接C++类和QML对象的方法,请在开发应用程序之前阅读此处。我在QML方面没有太多经验,但这很有帮助!不客气,如果你被困在某个地方,你可以看看这些例子。实际上,这个例子展示了如何做到这一点。声明和定义Person类。在main.cpp,调用engine.load(“your_qml.qml”)
之前,添加这段代码,qmlRegisterType(“your_module_name”,1/*模块的主要版本*/,2/*模块的次要版本*/,“Person/*qml type name*/)代码>因此它告诉qml引擎将Person
类暴露到qml
侧。您可以访问属性,也可以从qml调用公共插槽:
函数side@arnes好的!非常感谢你!我会和你一起玩,看看我能不能成功!如果不是太多的要求,你也请给我一个这样的C++模型的参考?就我个人而言,我并不是我选择的方法的粉丝,我只是这样做,因为我不知道其他方法:“RAAD,我看到你已经得到了关于C++模型的提示。回答您的另一个问题:您的属性不是qml文件的顶层(假设它是您在附加的.cpp文件中加载的),因为它位于您的qml文件中的一个组件中creates@Raad那真有趣。。。除了使用C++模型的建议之外,我想理解为什么失败,因为它实际上不应该。你能告诉我们你在哪里用C++代码读取属性吗?(在.cpp代码段中加载后连续或仅一次),这意味着您每次单击都要创建一个新的VideoCaptureControls
?在这种情况下,难怪你总是读0,你绝对应该遵循C++模型路线;这是一个一次性执行是的,但是,很可能您没有读取您认为应该读取的值(我假设您有一个正在运行的qml gui,并且每次单击按钮时都在创建一个不可见的对象)。关于可能性:您可能有机会获得更改后的信号,以便读取新值,或者您可以基于QTimer执行此操作,但实际上:不要;-)