You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QML Connections报错onValueChange不存在,C++信号无法更新QML文本

问题

尝试从C++代码向QML的main.qml发射信号更新标签文本时,出现错误:

QML Connections: Cannot assign to non-existent property "onValueChange"

且标签文本无法正常更新,尽管qDebug显示数值已正确变化。

相关代码如下:

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls
import LogicHandler 0.1
import "ui/LeftBar"
import "ui/MainTitle"
import "ui/CircleAnim"
import "ui/MathButtons"

Window {
    width: 1280
    height: 720

    LogicComp{
        id: logicClass
    }

    TextInput{
        id: textInput
        width: 40
        height: 20
    }

    //Equal button
    Button{
        id: equal
        width: 40
        height: 40
        onClicked: logicClass.callMe(textInput.text)
    }

    Text{
        id: label
        text: "Sample Text"

        Connections{
            target: logicClass
            onValueChange: label.text = OhmParallelOut
        }
    }
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QIcon>

#include <logic.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QGuiApplication::setWindowIcon(QIcon("TMC.ico"));

    qmlRegisterType< logic > ( "LogicHandler", 0, 1, "LogicComp" );

    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/TLCQuick/Main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
        &app, []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

logic.h

#ifndef LOGIC_H
#define LOGIC_H
#include <QObject>

class logic : public QObject
{
    Q_OBJECT

public:
    float OhmParallelNum=0;
    explicit logic(QObject *parent = nullptr);

signals:
    void ValueChange(float OhmParallelOut);

public slots:
    void callMe(float input);
};

#endif // LOGIC_H

logic.cpp

#include "logic.h"
#include <QDebug>
#include <string>

logic::logic(QObject *parent): QObject{parent}
{

}

void logic::callMe(float input)
{
    if (OhmParallelNum == 0){
        OhmParallelNum = input;
        qDebug() << "First entry. Currently: "<< OhmParallelNum;
    }
    else{
        OhmParallelNum = (input*OhmParallelNum)/(input+OhmParallelNum);
        Q_EMIT ValueChange(OhmParallelNum);
        qDebug() << "Adding. Currently: "<< OhmParallelNum;
    }
}

解决方案

1. 修复信号命名匹配问题

Qt中QML识别信号的规则是:C++信号名需以小写字母开头,QML中对应on+首字母大写的信号名。原代码中C++信号ValueChange首字母大写,导致QML无法匹配onValueChange处理函数。

修改logic.h中的信号:

signals:
    void valueChange(float OhmParallelOut); // 首字母改为小写

同步修改logic.cpp中的信号发射代码:

Q_EMIT valueChange(OhmParallelNum);

2. 修正QML中信号参数的访问方式

QML的Connections中无法直接使用信号参数名,需通过函数参数或arguments访问。修改QML的Connections部分:

Connections{
    target: logicClass
    function onValueChange(outValue) {
        label.text = outValue.toString();
    }
}

3. 修复类型不匹配问题

TextInput.text是字符串类型,直接传递给接收float的callMe会导致转换错误,需在QML中转为数值:

Button{
    id: equal
    width: 40
    height: 40
    onClicked: logicClass.callMe(parseFloat(textInput.text))
}

可选优化:使用Q_PROPERTY实现属性绑定

如果希望代码更简洁,可将C++中的数值声明为Q_PROPERTY,让QML直接绑定属性,无需手动处理信号:

修改logic.h

#ifndef LOGIC_H
#define LOGIC_H
#include <QObject>

class logic : public QObject
{
    Q_OBJECT
    Q_PROPERTY(float ohmParallelNum READ ohmParallelNum NOTIFY ohmParallelNumChanged)

private:
    float m_OhmParallelNum = 0;

public:
    explicit logic(QObject *parent = nullptr);
    float ohmParallelNum() const { return m_OhmParallelNum; }

signals:
    void ohmParallelNumChanged(float value);

public slots:
    void callMe(float input);
};

#endif // LOGIC_H

修改logic.cpp

#include "logic.h"
#include <QDebug>

logic::logic(QObject *parent): QObject{parent}
{

}

void logic::callMe(float input)
{
    if (m_OhmParallelNum == 0){
        m_OhmParallelNum = input;
        qDebug() << "First entry. Currently: "<< m_OhmParallelNum;
        emit ohmParallelNumChanged(m_OhmParallelNum);
    }
    else{
        m_OhmParallelNum = (input*m_OhmParallelNum)/(input+m_OhmParallelNum);
        emit ohmParallelNumChanged(m_OhmParallelNum);
        qDebug() << "Adding. Currently: "<< m_OhmParallelNum;
    }
}

修改QML中的Text组件

直接绑定属性,无需Connections:

Text{
    id: label
    text: logicClass.ohmParallelNum.toString() || "Sample Text"
}

内容的提问来源于stack exchange,提问作者Francesco Suardi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:40:54