基类以外的Qt属性

时间:2016-08-14 22:42:43

标签: c++ qt qml

我在我的C ++应用程序中使用QML前端到目前为止工作正常。但是,我计划整理我的代码并将函数拆分为更小的类

起初,我的财产损失看起来像这样:

class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude MEMBER baroAltitude NOTIFY pressureChanged)

public:
explicit mainBoard(QObject *parent = 0);
void start();

private:
   double baroAltitude = 0;
signals:
   void pressureChanged();
};

现在,我有了这个外部类,使用了我的getter方法。

#include "pressuresensor.h"

class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ pressureSensors.getBaroAltitude NOTIFY pressureSensors.pressureChanged)

public:
explicit mainBoard(QObject *parent = 0);
void start();

private:
   pressureSensor pressureSensors;
};

但是现在,我得到的只是:

mainboard.h:25: Parse error at "pressureSensors"
error: [moc_mainboard.cpp] Error 1

是否有更好的或正确的(因为它的工作:D)方式呢?

谢谢!

1 个答案:

答案 0 :(得分:1)

Q_PROPERTY不支持getter / setters方法,这些方法不属于相关类 如果你真的想保留pressureSensor课程,你必须在mainBoard课程中提供getter / setter并转发电话。

class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ getBaroAltitude)

public:
double getBaroAltitude() const {
    return pressureSensors.getBaroAlitude();
}

private:
   pressureSensor pressureSensors;
};