QML编程之Q_PROPERTY

目的:为什么需要使用Q_PROPERTY呢?因为我们在C++中进行逻辑处理的时候,会改变默写状态值,这些状态的改变,我们的UI界面也要同步更改,因此我采用Q_PROPERTY来进行属性绑定使C++和QML的状态同步。

关于Q_PROPERTY的介绍,网上有很多,我不进行介绍,我主要介绍的是在使用中需要注意的地方。在本文中我主要介绍三个我主要使用的内容。READ , WRITE, NOTIFY. 具体的使用注意事项都在实际的使用代码中说明。

C++ 代码:

#include "testproperty.h"

std::mutex TestProperty::s_mutexObj;
TestProperty* TestProperty::s_instance = NULL;
TestProperty::TestProperty()
{

}
TestProperty::~TestProperty()
{


}

void TestProperty::getQmlInfo()
{
    static int num = 0;
    if(num<=3)
        num++;
    else
        num = 0;
    //注意:此处不能直接使用 colorValue = num;但是在QML中我们可以直接使用
    setColorValue(num);
}
int TestProperty::getColorValue()
{
    return colorValue;
}
void TestProperty::setColorValue(int value)
{
    colorValue = value;
    emit colorValueChanged(colorValue);
}
TestProperty* TestProperty::GetInstance

猜你喜欢

转载自blog.csdn.net/dreamliweiming/article/details/132445157