简体   繁体   English

无法从QML访问C ++ QObject子类方法

[英]Can't access C++ QObject subclass methods from QML

Yesterday I was asked to recreate a regular QT form using QML (which was my first attempt ever using QLM). 昨天,我被要求使用QML重新创建常规的QT表单(这是我第一次使用QLM进行尝试)。 Everything was going well until I tried using c++ methods in the QML. 一切都进行得很好,直到我尝试在QML中使用c ++方法。 This is obviously not the original code, but the scenario looks something like this: 显然这不是原始代码,但是场景看起来像这样:

I have a super class deriving from QObject, with some properties, methods and even virtual methods: 我有一个源自QObject的超类,其中包含一些属性,方法甚至虚拟方法:

class SuperClass : public QObject {
    Q_OBJECT
    Q_PROPERTY(QString someProperty READ someProperty WRITE setSomeProperty)

protected:
    QString m_someProperty;

public:
    QString someProperty(void){return m_someProperty;}  //get method
    void setSomeProperty(QString newValue){m_someProperty = newValue;}  //set method
    Q_INVOKABLE virtual QString printSomething(void) = 0;
}

And then I have a class deriving from the SuperClass (like a specialization) with some more specific properties and methods and of course the virtual methods implementations and stuff: 然后,我有了一个从SuperClass派生的类(例如专业化类),它具有一些更特定的属性和方法,当然还有虚拟方法的实现和内容:

class DerivedClass : public SuperClass {
Q_PROPERTY(QString someSpecificProperty READ someSpecificProperty WRITE setSomeSpecificProperty)

private:
    QString m_someSpecificProperty;

public:
    QString specificProperty(void){return m_someSpecificProperty;}  //get method
    void someSpecificProperty(QString newValue){m_someSpecificProperty = newValue;}  //set method
    QString printSomething(void){return QString("Something!");}  //SuperClass virtual method
    Q_INVOKABLE QString printSomethingSpecific(void){return QString("Something Specific!");}
}

OK, this is it! 好,就是这样! Now assuming that DerivedClass is instantiated and added to the QML context properly under the name of "DrvClass" for example and that I have some QML control like a TextField which has a 'text:' property: 现在,假设已实例化DerivedClass并将其正确地添加到QML上下文中,例如以“ DrvClass”的名称进行命名,并且我拥有一些QML控件,例如TextField,它具有'text:'属性:

text: DrvClass.someProperty

using MasterClass' properties, it works just fine. 使用MasterClass的属性,效果很好。

text: DrvClass.printSomething()

even using virtual methods from MasterClass' which are implemented in the derived class works fine. 即使使用在继承类中实现的MasterClass虚拟方法也可以正常工作。 but... 但...

text: DrvClass.someSpecificProperty

doesn't work and I get something like " Unable to assign [undefined] to QString " 无法正常工作,我收到类似“ 无法为QString分配[undefined] ”的信息

text: DrvClass.printSomethingSpecific()

also doesn't work! 也行不通! " TypeError: Property 'printSomethingSpecific' of object SuperClass() is not a function " And the weird part is that it says that it's not a function from the SuperClass, being the instantiated class the Derived one! TypeError:对象SuperClass()的属性'printSomethingSpecific'不是函数 ”而且,很奇怪的部分是,它说这不是SuperClass的函数,它是派生类的实例化类!

I've looked for similar errors, but most of the time is from people who just forgot to include the Q_OBJECT macro... Mine's there for sure! 我一直在寻找类似的错误,但是大多数时候都是那些忘了包含Q_OBJECT宏的人提供的……我的矿肯定在那里! It seems that QML doesn't like much classes deriving from other classes that derive from QObjects :-/ Probably something to do with the meta-object compiler who only looks for invokable methods where it finds the Q_OBJECT macro and not on it's subclasses! 似乎QML不喜欢从QObjects派生的其他类派生的很多类:-/可能与元对象编译器有关,后者仅在可找到方法的地方找到Q_OBJECT宏,而不在其子类上!

So what you guys think the solution for this might be? 那么你们认为解决方案可能是什么? I could just add the Q_OBJECT macro to the DerivedClasses instead of the SuperClass, but I really need the SuperClass to be a QObject because of signals and stuff! 我可以只将Q_OBJECT宏添加到DerivedClasses而不是SuperClass中,但是由于信号和东西的原因,我真的需要SuperClass成为QObject! So is there some other macro I have to add to the DerivedClass for the moc to 'see' it? 那么,是否有其他宏我必须添加到DerivedClass中才能让Moc“看到”它? Or is this just the fruit of inexperience and I'm doing a dumb mistake somewhere? 还是这仅仅是缺乏经验的结果,我在某个地方犯了一个愚蠢的错误?

DerivedClass is missing Q_OBJECT macro (it is not inherited!). DerivedClass缺少Q_OBJECT宏(它不是继承的!)。

Then simply run qmake again on your project & compile: it should work. 然后只需在项目上再次运行qmake并进行编译:它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM