【问题标题】:why property binding in dynamic component creation not working?为什么动态组件创建中的属性绑定不起作用?
【发布时间】:2021-03-31 18:53:36
【问题描述】:

信号 '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);
        }
    }

}

}

【问题讨论】:

  • 你的不绑定是什么意思?我看到recvClearPin 属性的类型是字符串。请澄清问题。

标签: qt dynamic binding qml components


【解决方案1】:

您没有在此行中绑定clearPinobject=component.createObject(rootRectId,{clearPin: recvClearPin});。相反,您使用名为“clearPin”的项目创建一个 Json 对象,该项目的当前值为“recvClearPin”。如果要绑定,请使用Qt.binding

object=component.createObject(rootRectId,{clearPin: Qt.binding(function() { return recvClearPing} )});

【讨论】:

    【解决方案2】:

    请提供信号 clearPinFromDevManager(const QString& pinn) 连接的更多信息。

    对于动态对象,您应该在 QML 中使用带有 cpp-objects 的连接语法。

    您需要在 QML 中注册包含 clearPinFromDevManager(const QString& pinn) 的类,然后将其连接到动态创建的对象或 onComleted-handler,如下所示:

    1. 在 CPP/H 中声明你的班级:

      class CrearPinObject : public QObject {
         Q_OBJECT
      
         signals:
            void clearPinFromDevManager(const QString& pinn);
      }
      
    2. 注册QML引擎,main.cpp(或类似):

      ...
      
      QQmlApplicationEngine engine;
      
      engine.rootContext()->setContextProperty("CrearPinObjectView", new CrearPinObject);
      
      ...
      
    3. 使用 QML 连接语法 (PasswordWindow.qml) 连接信号:

      Item {
      
         Component.onCompleted: {
      
           // CrearPinObjectView-object forward automatically
           CrearPinObjectView.clearPinFromDevManager.connect(clearPinFromDevManager);
         }
      
         function clearPinFromDevManager(pinn) {
      
            console.log(pinn);
         }
      }
      

    希望有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2011-11-28
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2018-10-31
      • 2011-01-18
      相关资源
      最近更新 更多