对象属性

#include <QCoreApplication>
#include <QObject>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    // 创建一个 QObject 实例
    QObject obj;

    // 设置属性
    obj.setProperty("myInteger", 42);
    obj.setProperty("myString", "Hello, Qt!");
    obj.setProperty("myBool", true);

    // 获取属性
    int intValue = obj.property("myInteger").toInt();
    QString stringValue = obj.property("myString").toString();
    bool boolValue = obj.property("myBool").toBool();

    // 打印属性值
    qDebug() << "Integer property:" << intValue;
    qDebug() << "String property:" << stringValue;
    qDebug() << "Boolean property:" << boolValue;

    return app.exec();
}
Table of Contents