我只是 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/