Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML引用错误_Qt_Reference_Qml_Qt Creator - Fatal编程技术网

Qt QML引用错误

Qt QML引用错误,qt,reference,qml,qt-creator,Qt,Reference,Qml,Qt Creator,我有一个小的QML项目,我面临着QML组件引用的问题。所以我试图从main.qml中的startButton启动NumComponent.qml的numberTimer main.qml import QtQuick 2.7 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") NumComponent{} /

我有一个小的QML项目,我面临着QML组件引用的问题。所以我试图从main.qml中的startButton启动NumComponent.qml的numberTimer

main.qml

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    NumComponent{} //my component written in NumComponent.qml

    Rectangle{
        id: startButton
        anchors.centerIn: parent
        height: parent.height * 0.2
        width: height
        color: "lightblue"

        MouseArea{
            anchors.fill: parent
            onClicked: {
                numberTimer.start();
            }
        }
    }
}
import QtQuick 2.0

Rectangle {
    id: numberRect
    color: "red"
    height: parent.height * 0.4
    width: height

    Text{
        id: numberText
        anchors.centerIn: parent
        text: ""
    }
    Timer{
        id: numberTimer
        interval: 100
        repeat: true
        onTriggered: {
            numberText.text = Math.floor(Math.random() * 8);
        }
    }
}
NumComponent.qml

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    NumComponent{} //my component written in NumComponent.qml

    Rectangle{
        id: startButton
        anchors.centerIn: parent
        height: parent.height * 0.2
        width: height
        color: "lightblue"

        MouseArea{
            anchors.fill: parent
            onClicked: {
                numberTimer.start();
            }
        }
    }
}
import QtQuick 2.0

Rectangle {
    id: numberRect
    color: "red"
    height: parent.height * 0.4
    width: height

    Text{
        id: numberText
        anchors.centerIn: parent
        text: ""
    }
    Timer{
        id: numberTimer
        interval: 100
        repeat: true
        onTriggered: {
            numberText.text = Math.floor(Math.random() * 8);
        }
    }
}
我得到这个错误:“qrc:/main.qml:22:ReferenceError:NumberError未定义”

  • 为main.qml中的NumComponent指定一个id:

    NumComponent{
      id: numComponent
    } //my component written in NumComponent.qml
    
  • 将onClicked处理程序更改为:

    numComponent.startTimer()

  • 另一种变体:

  • 添加到您的号码更正属性别名:

    property alias timed: numberTimer.running
    
  • 将您在main中单击的处理程序更改为:

    numComponent.timed=!numComponent.timed

  • 添加到根项目中的NumComponent.qml:

    函数startTimer(){ numberTimer.start(); }

  • 现在您可以启动和停止计时器

  • 为main.qml中的NumComponent指定一个id:

    NumComponent{
      id: numComponent
    } //my component written in NumComponent.qml
    
  • 将onClicked处理程序更改为:

    numComponent.startTimer()

  • 另一种变体:

  • 添加到您的号码更正属性别名:

    property alias timed: numberTimer.running
    
  • 将您在main中单击的处理程序更改为:

    numComponent.timed=!numComponent.timed

  • 添加到根项目中的NumComponent.qml:

    函数startTimer(){ numberTimer.start(); }


  • 现在您可以启动和停止计时器。

    这是因为NumComponent是一个封闭的对象。您可以将numberRect(或您的numberTimer)用作或公开为,这是因为NumComponent是一个封闭的对象。您可以使用或公开numberRect(或您的numberTimer)作为