Qt枚举-setProperty隐式转换
考虑以下类别:Qt枚举-setProperty隐式转换,qt,properties,enums,metaclass,Qt,Properties,Enums,Metaclass,考虑以下类别: class foo : public QObject { Q_OBJECT Q_ENUMS(E) Q_PROPERTY(E x READ x WRITE setx) public: enum E { a = 0, b = 1, c = 2 }; } foo f; f->setProperty("x", 2); 不向setx提供映射的枚举值 有没有办法启用此功能 您仍然需要实际的get和set方法
class foo : public QObject {
Q_OBJECT
Q_ENUMS(E)
Q_PROPERTY(E x READ x WRITE setx)
public:
enum E {
a = 0,
b = 1,
c = 2
};
}
foo f;
f->setProperty("x", 2);
不向setx提供映射的枚举值
有没有办法启用此功能 您仍然需要实际的get和set方法:
class Foo : public QObject {
Q_OBJECT
Q_ENUMS(E)
Q_PROPERTY(E x READ x WRITE set_x)
public:
enum E {
a = 0,
b = 1,
c = 2
};
E x() const { return x_; }
void set_x(E value) { x_ = value; }
private:
E x_;
};
现在这将起作用:
int main (int argc, char **argv) {
QCoreApplication app(argc, argv);
Foo f;
f.setProperty("x", Foo::c);
std::cout << f.property("x").toInt() << std::endl; // 2
f.setProperty("x", 0); // Foo:a will also work
std::cout << f.property("x").toInt() << std::endl; // 0
}
int main(int argc,char**argv){
QCore应用程序应用程序(argc、argv);
福福;
f、 setProperty(“x”,Foo::c);
std::我可以结束这个问题,然后用更多的细节重新打开它吗(我完全知道如何编写访问器,但我想我们需要一个更简单的示例来处理和理解这些问题):