1

我看不出我做错了什么。我正在尝试在一个类中创建一些属性(就像我之前所做的那样),但这次我收到错误“类 Foo 没有名为 MyProp 的成员”

标题是:

#ifndef P_H
#define P_H

#include <QObject>

class P : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int Prop READ getProp WRITE setProp)
  public:
    explicit P(QObject *parent = 0);

    int getProp() const;
    void setProp(int nP);
  private:
    int m_p;
};

#endif // P_H

cpp文件是:

#include "p.h"

P::P(QObject *parent) :
  QObject(parent)
{
}

int P::getProp() const
{
  return m_p;
}

void P::setProp(int nP)
{
  m_p = nP;
}

但是当我尝试使用 foobar.PI 得到错误类 P 没有名为 P 的成员。我一直在阅读Qt 文档,但看不出有什么区别。有谁看到我做错了什么?

我正在使用 Qt Creator 2.4.1 和 Qt 4.8。

[... 编辑 ...]

这是我尝试使用它的方式:

#include "p.h"

int main(int argc, char *argv[])
{
  P c;

  c.Prop = 2;

  return 0;
}

这是我能想到的最简单的例子,我得到了同样的错误。

提前致谢。

4

1 回答 1

3

你需要像这样使用它:

P c;
c.setProperty("Prop", 42);  // set the property
c.property("Prop");         // retrieve the property
于 2012-10-05T09:52:50.623 回答