c++ - Qt QML - 无法分配给不存在的属性 "onClicked"

标签 c++ qt qml qtquick2

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

这是我的代码,如果您需要更多,请告诉我,如果我错过了一些明显的东西或者我需要更具体的话,抱歉

祝你有美好的一天!

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();
}

最佳答案

您必须在项目内创建连接,例如:

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()
}

关于c++ - Qt QML - 无法分配给不存在的属性 "onClicked",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560554/

相关文章:

c++ - 提供 optional 参数的现代 C++ 方法

c++ - 未看到包含的头文件

qt - 如何检测窗口是否已被激活?

c++ - 对列表进行排序并返回其原始顺序或下标

c++ - 如何在 C++ 中获取中继器的委托(delegate)数据?

c++ - 外部函数如何使用设备内存中的变量?

c++ - boost单元测试框架中的日志级别

c++ - Qt5上的静态库 : Undefined symbols for architecture x86_64:

c++ - 当 QML 中的父属性更改时更改子属性

xml - 有没有办法让 XmlListModel 不显示 XmlRole 为空的项目?