自定义QML类型中“depends on non-NOTIFYable properties”的问题

 上下文

自定义的类型PieChart

//piechart.h
#ifndef PIECHART_H
#define PIECHART_H

//![0]
#include <QtQuick/QQuickPaintedItem>
#include <QColor>

class PieChart : public QQuickPaintedItem
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName)
    Q_PROPERTY(QColor color READ color WRITE setColor)

public:
    PieChart(QQuickItem *parent = 0);

    QString name() const;
    void setName(const QString &name);

    QColor color() const;
    void setColor(const QColor &color);

    void paint(QPainter *painter);

private:
    QString m_name;
    QColor m_color;
};
//![0]

#endif
//piechart.cpp
#include "piechart.h"
#include <QPainter>

//![0]
PieChart::PieChart(QQuickItem *parent)
    : QQuickPaintedItem(parent)
{
}
//![0]

QString PieChart::name() const
{
    return m_name;
}

void PieChart::setName(const QString &name)
{
    m_name = name;
}

QColor PieChart::color() const
{
    return m_color;
}

void PieChart::setColor(const QColor &color)
{
    m_color = color;
}

//![1]
void PieChart::paint(QPainter *painter)
{
    QPen pen(m_color, 2);
    painter->setPen(pen);
    painter->setRenderHints(QPainter::Antialiasing, true);
    painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);
}
//![1]

 通过qmlRegisterType注册

qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");

qml中使用 

//app.qml
import Charts 1.0
import QtQuick 2.0

Item {
    width: 300; height: 200

    PieChart {
        id: aPieChart
        anchors.centerIn: parent
        width: 100; height: 100
        name: qsTr("A simple pie chart")
 // @disable-check M16
        color: "red"
    }

    Text {
        y: 140
        text: aPieChart.name
        anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 48 }
        anchors.horizontalCenterOffset: -72
    }

    Text {
        id: text1
        x: 150
        y: 165
        width: 105
        height: 15
        text: aPieChart.name
        font.pixelSize: 12
    }
}

代码中Text对象中引用了PieChart对象中的name属性,由于name属性被引用,运行时报错

QQmlExpression: Expression qrc:///app.qml:79:15 depends on non-NOTIFYable properties:
    PieChart::name
QQmlExpression: Expression qrc:///app.qml:68:15 depends on non-NOTIFYable properties:
    PieChart::name

原因

属性name被引用,当name值被修改时,需要通知所有引用该字段用于更新,但未定义NOTIFY属性

解决办法

添加name属性的NOTIFY属性和信号定义

//piechart.h Modified
#ifndef PIECHART_H
#define PIECHART_H

//![0]
#include <QtQuick/QQuickPaintedItem>
#include <QColor>

class PieChart : public QQuickPaintedItem
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QColor color READ color WRITE setColor)

public:
    PieChart(QQuickItem *parent = 0);

    QString name() const;
    void setName(const QString &name);

    QColor color() const;
    void setColor(const QColor &color);

    void paint(QPainter *painter);

signals:
    void nameChanged();

private:
    QString m_name;
    QColor m_color;
};
//![0]

#endif

参考

  1. C++与QML交互,把C++对象特性暴露到QML中
  2. 关于QML中非NOTIFYable属性的警告——QML

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值