qt core 提供了元对象系统,扩展了c++ 在元对象系统的基础上,qt又提供了信号/槽、property以及对象树等特性
- The Meta-Object System
- The Property System
- Object Model
- Object Trees & Ownership
- Signals & Slots
Meta-Object系统
较复杂,暂时跳过
Property 系统
例子:
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
public:
MyClass(QObject *parent = nullptr);
~MyClass();
enum Priority { High, Low, VeryHigh, VeryLow };
Q_ENUM(Priority)
void setPriority(Priority priority)
{
m_priority = priority;
emit priorityChanged(priority);
}
Priority priority() const
{ return m_priority; }
signals:
void priorityChanged(Priority);
private:
Priority m_priority;
};
信号 & 槽
一个普通的c++类:
class Counter
{
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
void setValue(int value);
private:
int m_value;
};
添加信号槽:
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
信号 & 槽的连接用connect函数:
connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(Qbject*)));
connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed()));
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed()));
标签:Priority,QT,int,value,QObject,priority,模块,QtCore,public
From: https://www.cnblogs.com/feipeng8848/p/17029102.html