Qt QML-无法分配给不存在的属性“ onClicked”

时间:2018-11-30 15:37:28

标签: c++ qt qml qtquick2

我只是Qt的初学者,我想做一个简单的加法计算器。我尝试运行它并遇到此问题:“ QQmlApplicationEngine无法加载组件qrc:/main.qml:74无法分配给不存在的属性“ onClicked”。.onTextChanged也发生了(当我评论有问题的部分时。)< / p>

这是我的代码,请告诉我您是否还需要更多,对不起,如果我错过了明显的内容或需要更具体地说明

祝你有美好的一天!

import QtQuick 2.9
import QtQuick.Window 2.3
import QtQuick.Controls 2.3
import Qt.calculatrice 1.0

Window {
id: windo
visible: true
width: 500
height: 300
title: qsTr("Calculatrice")
property alias int1: int1
property alias int2: int2
property alias buttonEgal: buttonEgal

Calculatrice{
    id: calculatrice;
}

Button {
    id: buttonEgal
    x: 277
    y: 130
    text: qsTr("=")
}

Label {
    id: labelResultat
    x: 383
    y: 130
    width: 99
    height: 40
    text: qsTr("Resultat")
    verticalAlignment: Text.AlignVCenter
    horizontalAlignment: Text.AlignHCenter
}

ComboBox {
    id: comboBoxOperator
    x: 104
    y: 130
    width: 70
    height: 40
    textRole: ""
}

TextField {
    id: int1
    x: 17
    y: 130
    width: 81
    height: 40
    text: qsTr("Text Field")
}

TextField {
    id: int2
    x: 188
    y: 130
    width: 83
    height: 40
    text: qsTr("Text Field")
}


int1.onTextChanged:{
    calculatrice.int1 = int1.text
}

int2.onTextChanged: {
    calculatrice.int2 = int2.text
}

buttonEgal.onClicked:{
     calculatrice.addition();
}

}

这是我的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>
#include "calculatrice.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    Calculatrice calculatrice;

    QQuickStyle::setStyle("Material");

    qmlRegisterType<Calculatrice>("Qt.calculatrice", 1, 0, "Calculatrice");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

1 个答案:

答案 0 :(得分:1)

您必须在Item中创建连接,例如:

Button {
    id: buttonEgal
    x: 277
    y: 130
    text: qsTr("=")
    onClicked: calculatrice.addition()
}

完整代码:

Window {
    id: windo
    visible: true
    width: 500
    height: 300
    title: qsTr("Calculatrice")
    property alias int1: int1
    property alias int2: int2
    property alias buttonEgal: buttonEgal

    Calculatrice{
        id: calculatrice
    }

    Button {
        id: buttonEgal
        x: 277
        y: 130
        text: qsTr("=")
        onClicked: calculatrice.addition()
    }

    Label {
        id: labelResultat
        x: 383
        y: 130
        width: 99
        height: 40
        text: qsTr("Resultat")
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }

    ComboBox {
        id: comboBoxOperator
        x: 104
        y: 130
        width: 70
        height: 40
        textRole: ""
    }

    TextField {
        id: int1
        x: 17
        y: 130
        width: 81
        height: 40
        text: qsTr("Text Field")
        onTextChanged: calculatrice.int1 = int1.text
    }

    TextField {
        id: int2
        x: 188
        y: 130
        width: 83
        height: 40
        text: qsTr("Text Field")
        onTextChanged: calculatrice.int2 = int2.text
    }
}

或使用“连接”(尽管如果对象可访问,我不希望使用此选项):

Connections{
    target: int1
    onTargetChanged: calculatrice.int1 = int1.text
}

Connections{
    target: int2
    onTargetChanged: calculatrice.int2 = int2.text
}
Connections{
    target: buttonEgal
    onTargetChanged: calculatrice.addition()
}