Qt6 QML Book/Qt C++/The QObject

The QObject

As described in the introduction, the QObject is what enables mant of Qt's core functions such as signals and slots. This is implemented through introspection, which is what QObject provides. QObject is the base class of almost all classes in Qt. Exceptions are value types such as QColorQString and QList.

正如简介中所述,QoObject支持Qt的核心功能,如信号和槽。这是通过QObject提供的内省实现的。QObject是Qt中几乎所有类的基类。例外情况是值类型,如QColor、QString和QList。

A Qt object is a standard C++ object, but with more abilities. These can be divided into two groups: introspection and memory management. The first means that a Qt object is aware of its class name, its relationship to other classes, as well as its methods and properties. The memory management concept means that each Qt object can be the parent of child objects. The parent owns the children, and when the parent is destroyed, it is responsible for destroying its children.

Qt对象是标准C++对象,但具有更多的能力。这些可以分为两组:内省和内存管理。第一个意思是Qt对象知道它的类名、它与其他类的关系,以及它的方法和属性。内存管理概念意味着每个Qt对象都可以是子对象的父对象。父母拥有孩子,当父母被摧毁时,他们有责任摧毁自己的孩子。

The best way of understanding how the QObject abilities affect a class is to take a standard C++ class and Qt enables it. The class shown below represents an ordinary such class.

了解QObject能力如何影响一个类的最好方法是采取标准C++类,Qt使它生效。下面显示的类代表一个普通的此类。

The person class is a data class with a name and gender properties. The person class uses Qt’s object system to add meta information to the c++ class. It allows users of a person object to connect to the slots and get notified when the properties get changed.

person类是一个具有名称和性别属性的数据类。人物类使用Qt的对象系统向C++类添加元信息。它允许person对象的用户连接到槽,并在属性发生更改时收到通知。

class Person : public QObject
{
    Q_OBJECT // enabled meta object abilities

    // property declarations required for QML
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(Gender gender READ gender WRITE setGender NOTIFY genderChanged)

    // enables enum introspections
    Q_ENUMS(Gender)
    
    // makes the type creatable in QML
    QML_ELEMENT

public:
    // standard Qt constructor with parent for memory management
    Person(QObject *parent = 0);

    enum Gender { Unknown, Male, Female, Other };

    QString name() const;
    Gender gender() const;

public slots: // slots can be connected to signals, or called
    void setName(const QString &);
    void setGender(Gender);

signals: // signals can be emitted
    void nameChanged(const QString &name);
    void genderChanged(Gender gender);

private:
    // data members
    QString m_name;
    Gender m_gender;
};

The constructor passes the parent to the superclass and initializes the members. Qt’s value classes are automatically initialized. In this case QString will initialize to a null string (QString::isNull()) and the gender member will explicitly initialize to the unknown gender.

构造函数将父类传递给超类并初始化成员。Qt的值类会自动初始化。在这种情况下,QString将初始化为空字符串(QString::isNull()),gender成员将显式初始化为未知的gender。

Person::Person(QObject *parent)
    : QObject(parent)
    , m_gender(Person::Unknown)
{
}

The getter function is named after the property and is normally a basic const function. The setter emits the changed signal when the property has changed. To ensure that the value actually has changed, we insert a guard to compare the current value with the new value. Only when the value differs we assign it to the member variable and emit the changed signal.

getter函数以属性命名,通常是一个基本常量函数。当属性发生更改时,setter会发出更改的信号。为了确保值实际上已经更改,我们插入了一个保护来比较当前值和新值。只有当值不同时,我们才将其分配给成员变量并发出更改的信号。

QString Person::name() const
{
    return m_name;
}

void Person::setName(const QString &name)
{
    if (m_name != name) // guard
    {
        m_name = name;
        emit nameChanged(m_name);
    }
}

Having a class derived from QObject, we have gained more meta object abilities we can explore using the metaObject() method. For example, retrieving the class name from the object.

有了一个从QObject派生的类,我们获得了更多的元对象能力,可以使用metaObject()方法进行探索。例如,从对象中检索类名。

Person* person = new Person();
person->metaObject()->className(); // "Person"
Person::staticMetaObject.className(); // "Person"

There are many more features which can be accessed by the QObject base class and the meta object. Please check out the QMetaObject documentation.

QObject基类和元对象还可以访问更多功能。请查看QMetaObject文档。

TIP

QObject, and the Q_OBJECT macro has a lightweight sibling: Q_GADGET. The Q_GADGET macro can be inserted in the private section of non-QObject-derived classes to expose properties and invokable methods. Beware that a Q_GADGET object cannot have signals, so the properties cannot provide a change notification signal. Still, this can be useful to provide a QML-like interface to data structures exposed from C++ to QML without invoking the cost of a fully fledged QObject.

QObject,而Q_OBJECT宏有一个轻量级的兄弟:Q_GADGETQ_GADGET宏可以插入非QObject派生类的私有部分,以暴露属性和可调用的方法。请注意,Q_GADGET对象不能有信号,因此属性不能提供更改通知信号。尽管如此,这对于在不调用完全成熟的QObject的成本下提供从C++到QML的数据结构提供类似QML的接口是有用的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值