有兩種可能的方法:
你應該:
- 歧義暴露QML的名字。
- 將
Q_PROPERTY
中未給出的方法公開爲可引用的。
例如爲:
class Foo : public QObject {
Q_OBJECT
Q_PROPERTY(int Index READ Index WRITE setIndex NOTIFY IndexChanged)
// QML Interface
Q_INVOKABLE int Index1(int n) const { return Index(n); }
public:
// C++ interface
int Index() const;
int Index(int n) const;
void setIndex(int index);
Q_SIGNAL void IndexChanged();
};
另外,如果你在地方產權接口的使用功能界面從JavaScript OK,你需要通過QVariant
採取一個可選的參數。有關QML重載的更多詳細信息,請參閱this question。例如: -
class Foo : public QObject {
Q_OBJECT
Q_PROPERTY(int IndexProp READ Index WRITE setIndex NOTIFY IndexChanged)
// QML interface
Q_INVOKABLE int Index(const QVariant & n) const {
if (n.isValid())
return Index(n.toInt());
return Index();
}
public:
// C++ interface
int Index() const;
int Index(int n) const;
void setIndex(int index);
Q_SIGNAL void IndexChanged();
};
下都是有效的,那麼:
target.IndexProp = 5
foo = target.IndexProp
foo = target.Index()
foo = target.Index(2)
可以混合部件和READ提供兩個訪問,也許。 – skypjack