考虑以下 Qt 代码:
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", QVariant((int)1));
std::cout << f.property("x").toInt() << std::endl; // 1
f.setProperty("x", QVariant((long long)0));
std::cout << f.property("x").toInt() << std::endl; // should be 0. is 1.
}
为什么它会这样工作?