我正在为我的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)?

谢谢!

最佳答案

Q_PROPERTY不支持不属于所讨论类的getter / setter方法。
如果您确实想保留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;
};

关于c++ - 基类之外的Qt属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38947254/

10-15 13:42