c++中设置一个常量类-C++代码 (Constants.h)

时间:2024-04-17 19:21:03

cpp

#ifndef CONSTANTS_H  
#define CONSTANTS_H  
  
#include <QObject>  
  
class Constants : public QObject {  
    Q_OBJECT  
  
public:  
    explicit Constants(QObject *parent = nullptr);  
  
    // 常量属性,只读  
    Q_PROPERTY(int SomeConstant READ someConstant CONSTANT)  
    Q_PROPERTY(QString AnotherConstant READ anotherConstant CONSTANT)  
  
    int someConstant() const;  
    QString anotherConstant() const;  
  
private:  
    // 这些是类的常量成员,可以在构造函数中初始化  
    static const int SOME_CONSTANT_VALUE = 42;  
    static const QString ANOTHER_CONSTANT_VALUE = "Hello, QML!";  
};  
  
#endif // CONSTANTS_H