【发布时间】:2020-11-24 12:39:48
【问题描述】:
我正在尝试使用自定义类型 MoneyTst 作为类 tst 中的属性,这是一个 qobject。当我在 tst 实例上调用方法 setProperty(amount,8000) 时,它不会将值分配给属性。你能解释一下为什么这不是在这个属性上设置它的值吗?
//我正在尝试使用QObject.setProperty()设置的自定义类型
struct MoneyTst{
MoneyTst(){}
MoneyTst(int value){
this->value = value;
}
int value;
int getValue() const{
return this->value;
}
void registerConverter(){
QMetaType::registerConverter(&MoneyTst::getValue);
}
};
Q_DECLARE_METATYPE(MoneyTst)
class tst : public QObject{
Q_OBJECT
//Using MoneyTst over here as property
Q_PROPERTY(MoneyTst amount READ getAmount WRITE setAmount)
public:
MoneyTst getAmount() const{
return this->amount;
}
void setAmount(MoneyTst value){
this->amount = value;
}
private:
MoneyTst amount;
};
void runTest{
tst o;
o.setProperty("amount",8000);
QVERIFY(o.property("amount").toInt() == 8000); //Fails because not value is not setting to 8000.
}
【问题讨论】: