我可能会遗漏一些明显的东西,但是当试图将Q_ENUM暴露给QML时,即使是在最简单的情况下,也似乎没有像QT文档中那样工作(http://doc.qt.nokia.com/4.7 -snapshot/qtbinding.html#using-enumerations-of-a-custom-type)
我创建了一个简单的测试用例,我的C++类看起来像:
class MyClass : public QDeclarativeItem {
Q_OBJECT
Q_ENUMS(testType)
public:
MyClass() : t(FirstValue) { }
enum testType { InvalidValue, FirstValue, SecondValue } ;
testType testVal() const { return t; }
Q_PROPERTY(testType testVal READ testVal NOTIFY testValChanged)
private:
testType t;
signals:
void testValChanged();
};
Run Code Online (Sandbox Code Playgroud)
然后我注册并将此类的实例注入QDeclartiveContext.
当我尝试访问testVal属性时,它返回整数(在本例中为1)而不是字符串表示.另外,如果实例注入'aVar',如果我尝试访问'aVar.FirstValue',结果是'undefined'
所以这意味着我不能做以下测试:( 'if aVar.testVal == FirstValue' 不合格的FirstValue的ReferenceError)
或者是这样的:'if aVar.testVal == aVar.FirstValue'(aVar.FirstValue未定义)
以前有人经历过这个吗?它似乎与QT文档中提供的示例冲突,但是,在该示例中,Object是从QML实例化的,因此这可能是原因..
这有效:
public static class SomeGenericType<T> {
private TypeLiteral<T> type;
@Inject
public SomeGenericType(TypeLiteral<T> type) {
this.type = type;
}
public Class<? super T> getType() {
return type.getRawType();
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,Guice自动注入表示字符串的TypeLiteral:
@Inject SomeGenericType<String> foo;
Run Code Online (Sandbox Code Playgroud)
但是在使用Assisted Inject尝试相同的操作时:
public static interface FooFactory<T> {
Foo<T> create(String name);
}
public static class Foo<T> {
@AssistedInject
public Foo(TypeLiteral<T> type, @Assisted String name) {
....
Run Code Online (Sandbox Code Playgroud)
我的模块如下所示:
public static class TestMod extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(new TypeLiteral<FooFactory<String>>(){}));
}
}
Run Code Online (Sandbox Code Playgroud)
安装模块时出现异常:
TypeLiteral<T> cannot be used …Run Code Online (Sandbox Code Playgroud)