为什么 属性 动态组件创建中的绑定不起作用?
why property binding in dynamic component creation not working?
信号 'clearPinFromDevManager(const QString& pinn)' 将从 cpp 文件中重复发出。信号在 ClearPinFromDevManager:{} 上捕获,因此 cpp 和 main.qml 文件中的所有内容都正常,但 'recvClearPin' 未绑定到动态创建的 PasswordWindow.qml 组件。
注意:如果代码有问题,您的解决方案是什么?谢谢
Main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import Terminal 1.0
import QtQuick.Controls 2.12
ApplicationWindow {
id: rootId
visible: true
objectName: "window"
width: Screen.width
height: Screen.height
flags: Qt.FramelessWindowHint
property var component
property var object
property string recvClearPin:""
Rectangle {
id:rootRectId
width: rootId.width
height: rootId.height - headerId.height
color: "#000033"
}
Terminal {
id: terminalId
onSignalToInitTerminal:{
console.log("show Password window.")
component= Qt.createComponent("Ui/PasswordWindow.qml");
object=component.createObject(rootRectId,{clearPin: recvClearPin});
}
//signal will emit repeatedly
onClearPinFromDevManager:{
console.log("signal emited here and pinn is:",pinn)
recvClearPin=pinn
}
}
}
PasswordWindow.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import "../Definitions.js" as Definitions
Item {
id:passwordWindowId
width: parent.width
property string password: ""
property alias clearPin: textEditId.text
Text {
id: passwordWindowTextId
font.family: Definitions.LABEL_FONT_FAMILY; font.pointSize: Definitions.LABEL_POINT_SIZE
text: qsTr("لطفا رمز خود را وارد نمایید")
color: Definitions.LABEL_COLOR
y: Definitions.PAGE_TITLE_Y_POS
x: Definitions.PAGE_TITLE_X_POS
}
Rectangle {
id: textEditRectId
x: passwordWindowTextId.x +10
y: passwordWindowTextId.y + height
width: Definitions.TEXT_EDIT_WIDTH
height: Definitions.TEXT_EDIT_HEIGHT
border.width: 1
border.color: "#000000"
radius: 10
color: Definitions.GENERAL_COLOR
TextEdit {
id: textEditId
width: parent.width
height: parent.height
text: ""
font.family: Definitions.GENERAL_FONT_FAMILY; font.pointSize: Definitions.GENERAL_POINT_SIZE
color: "blue"
focus: true
verticalAlignment: TextEdit.AlignVCenter
horizontalAlignment: TextEdit.AlignHCenter
onTextChanged: {
console.log("texttttttttttttttttt:",text);
}
}
}
}
请提供更多信息,其中连接了信号 clearPinFromDevManager(const QString& pinn)。
对于动态对象,您应该在 QML 中使用带有 cpp 对象的连接语法。
您需要在 QML 中注册包含 clearPinFromDevManager(const QString& pinn) 的 class,然后将其连接到动态创建的对象,或者在 onComleted-handler 中,例如这个:
在CPP/H中声明你的class:
class CrearPinObject : public QObject {
Q_OBJECT
signals:
void clearPinFromDevManager(const QString& pinn);
}
注册QML引擎,main.cpp(或类似):
...
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("CrearPinObjectView", new CrearPinObject);
...
使用 QML 连接语法连接信号 (PasswordWindow.qml):
Item {
Component.onCompleted: {
// CrearPinObjectView-object forward automatically
CrearPinObjectView.clearPinFromDevManager.connect(clearPinFromDevManager);
}
function clearPinFromDevManager(pinn) {
console.log(pinn);
}
}
希望有所帮助!
您没有在这一行中绑定 clearPin
:object=component.createObject(rootRectId,{clearPin: recvClearPin});
。相反,您使用名为 'clearPin' 的项目创建一个 Json 对象,该项目的当前值为 'recvClearPin'。如果需要绑定,请使用 Qt.binding
:
object=component.createObject(rootRectId,{clearPin: Qt.binding(function() { return recvClearPing} )});
信号 'clearPinFromDevManager(const QString& pinn)' 将从 cpp 文件中重复发出。信号在 ClearPinFromDevManager:{} 上捕获,因此 cpp 和 main.qml 文件中的所有内容都正常,但 'recvClearPin' 未绑定到动态创建的 PasswordWindow.qml 组件。
注意:如果代码有问题,您的解决方案是什么?谢谢
Main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import Terminal 1.0
import QtQuick.Controls 2.12
ApplicationWindow {
id: rootId
visible: true
objectName: "window"
width: Screen.width
height: Screen.height
flags: Qt.FramelessWindowHint
property var component
property var object
property string recvClearPin:""
Rectangle {
id:rootRectId
width: rootId.width
height: rootId.height - headerId.height
color: "#000033"
}
Terminal {
id: terminalId
onSignalToInitTerminal:{
console.log("show Password window.")
component= Qt.createComponent("Ui/PasswordWindow.qml");
object=component.createObject(rootRectId,{clearPin: recvClearPin});
}
//signal will emit repeatedly
onClearPinFromDevManager:{
console.log("signal emited here and pinn is:",pinn)
recvClearPin=pinn
}
}
}
PasswordWindow.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import "../Definitions.js" as Definitions
Item {
id:passwordWindowId
width: parent.width
property string password: ""
property alias clearPin: textEditId.text
Text {
id: passwordWindowTextId
font.family: Definitions.LABEL_FONT_FAMILY; font.pointSize: Definitions.LABEL_POINT_SIZE
text: qsTr("لطفا رمز خود را وارد نمایید")
color: Definitions.LABEL_COLOR
y: Definitions.PAGE_TITLE_Y_POS
x: Definitions.PAGE_TITLE_X_POS
}
Rectangle {
id: textEditRectId
x: passwordWindowTextId.x +10
y: passwordWindowTextId.y + height
width: Definitions.TEXT_EDIT_WIDTH
height: Definitions.TEXT_EDIT_HEIGHT
border.width: 1
border.color: "#000000"
radius: 10
color: Definitions.GENERAL_COLOR
TextEdit {
id: textEditId
width: parent.width
height: parent.height
text: ""
font.family: Definitions.GENERAL_FONT_FAMILY; font.pointSize: Definitions.GENERAL_POINT_SIZE
color: "blue"
focus: true
verticalAlignment: TextEdit.AlignVCenter
horizontalAlignment: TextEdit.AlignHCenter
onTextChanged: {
console.log("texttttttttttttttttt:",text);
}
}
}
}
请提供更多信息,其中连接了信号 clearPinFromDevManager(const QString& pinn)。
对于动态对象,您应该在 QML 中使用带有 cpp 对象的连接语法。
您需要在 QML 中注册包含 clearPinFromDevManager(const QString& pinn) 的 class,然后将其连接到动态创建的对象,或者在 onComleted-handler 中,例如这个:
在CPP/H中声明你的class:
class CrearPinObject : public QObject { Q_OBJECT signals: void clearPinFromDevManager(const QString& pinn); }
注册QML引擎,main.cpp(或类似):
... QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("CrearPinObjectView", new CrearPinObject); ...
使用 QML 连接语法连接信号 (PasswordWindow.qml):
Item { Component.onCompleted: { // CrearPinObjectView-object forward automatically CrearPinObjectView.clearPinFromDevManager.connect(clearPinFromDevManager); } function clearPinFromDevManager(pinn) { console.log(pinn); } }
希望有所帮助!
您没有在这一行中绑定 clearPin
:object=component.createObject(rootRectId,{clearPin: recvClearPin});
。相反,您使用名为 'clearPin' 的项目创建一个 Json 对象,该项目的当前值为 'recvClearPin'。如果需要绑定,请使用 Qt.binding
:
object=component.createObject(rootRectId,{clearPin: Qt.binding(function() { return recvClearPing} )});