-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathCppObject.h
45 lines (37 loc) · 1.26 KB
/
CppObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef CPPOBJECT_H
#define CPPOBJECT_H
#include <QObject>
//派生自QObject
//使用qmlRegisterType注册到QML中
class CppObject : public QObject
{
Q_OBJECT
//注册属性,使之可以在QML中访问--具体语法百度Q_PROPERTY
Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
Q_PROPERTY(int year READ getYear WRITE setYear NOTIFY yearChanged)
public:
explicit CppObject(QObject *parent = nullptr);
//通过Q_INVOKABLE宏标记的public函数可以在QML中访问
Q_INVOKABLE void sendSignal();//功能为发送信号
//给类属性添加访问方法--myName
void setName(const QString &name);
QString getName() const;
//给类属性添加访问方法--myYear
void setYear(int year);
int getYear() const;
signals:
//信号可以在QML中访问
void cppSignalA();//一个无参信号
void cppSignalB(const QString &str,int value);//一个带参数信号
void nameChanged(const QString name);
void yearChanged(int year);
public slots:
//public槽函数可以在QML中访问
void cppSlotA();//一个无参槽函数
void cppSlotB(const QString &str,int value);//一个带参数槽函数
private:
//类的属性
QString myName;
int myYear;
};
#endif // CPPOBJECT_H