自己的一点记录,非教程,比较乱, 以 simple 为例子。
1、 QtVariantPropertyManager *variantManager = new QtVariantPropertyManager();
创建QtVariantPropertyManager,在类构造函数中创建了各个QT本地类型的PropertyManager,记录下4种信息:
1、d_ptr->m_typeToPropertyManager[QVariant::Int] = intPropertyManager;
2、d_ptr->m_typeToAttributeToAttributeType[QVariant::Int][d_ptr->m_minimumAttribute] = QVariant::Int; 相当于属性的子属性信息。
3、d_ptr->m_typeToValueType[QVariant::Int] = QVariant::Int;
4、connect(intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)), this, SLOT(slotValueChanged(QtProperty *, int))); 该属性特有的信号和槽。
下面代码记录了改构造函数的开始部分,并包括 int double 两类属性。
d_ptr = new QtVariantPropertyManagerPrivate;
d_ptr->q_ptr = this;
d_ptr->m_creatingProperty = false;
d_ptr->m_creatingSubProperties = false;
d_ptr->m_destroyingSubProperties = false;
d_ptr->m_propertyType = 0;
// IntPropertyManager
QtIntPropertyManager *intPropertyManager = new QtIntPropertyManager(this);
d_ptr->m_typeToPropertyManager[QVariant::Int] = intPropertyManager;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Int][d_ptr->m_minimumAttribute] = QVariant::Int;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Int][d_ptr->m_maximumAttribute] = QVariant::Int;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Int][d_ptr->m_singleStepAttribute] = QVariant::Int;
d_ptr->m_typeToValueType[QVariant::Int] = QVariant::Int;
connect(intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)),
this, SLOT(slotValueChanged(QtProperty *, int)));
connect(intPropertyManager, SIGNAL(rangeChanged(QtProperty *, int, int)),
this, SLOT(slotRangeChanged(QtProperty *, int, int)));
connect(intPropertyManager, SIGNAL(singleStepChanged(QtProperty *, int)),
this, SLOT(slotSingleStepChanged(QtProperty *, int)));
// DoublePropertyManager
QtDoublePropertyManager *doublePropertyManager = new QtDoublePropertyManager(this);
d_ptr->m_typeToPropertyManager[QVariant::Double] = doublePropertyManager;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Double][d_ptr->m_minimumAttribute] =
QVariant::Double;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Double][d_ptr->m_maximumAttribute] =
QVariant::Double;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Double][d_ptr->m_singleStepAttribute] =
QVariant::Double;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Double][d_ptr->m_decimalsAttribute] =
QVariant::Int;
d_ptr->m_typeToValueType[QVariant::Double] = QVariant::Double;
connect(doublePropertyManager, SIGNAL(valueChanged(QtProperty *, double)),
this, SLOT(slotValueChanged(QtProperty *, double)));
connect(doublePropertyManager, SIGNAL(rangeChanged(QtProperty *, double, double)),
this, SLOT(slotRangeChanged(QtProperty *, double, double)));
connect(doublePropertyManager, SIGNAL(singleStepChanged(QtProperty *, double)),
this, SLOT(slotSingleStepChanged(QtProperty *, double)));
connect(doublePropertyManager, SIGNAL(decimalsChanged(QtProperty *, int)),
this, SLOT(slotDecimalsChanged(QtProperty *, int)));
2、创建一个组属性:groupTypeId(),
QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
QString::number(i++) + QLatin1String(" Group Property"));
QtVariantProperty *QtVariantPropertyManager::addProperty(int propertyType, const QString &name)
{
if (!isPropertyTypeSupported(propertyType))
return 0;
bool wasCreating = d_ptr->m_creatingProperty;
d_ptr->m_creatingProperty = true;
d_ptr->m_propertyType = propertyType;
QtProperty *property = QtAbstractPropertyManager::addProperty(name);
d_ptr->m_creatingProperty = wasCreating;
d_ptr->m_propertyType = 0;
if (!property)
return 0;
return variantProperty(property);
}
2.1 QtProperty *property = QtAbstractPropertyManager::addProperty(name); 这里调用基类创建特性。
QtProperty *QtAbstractPropertyManager::addProperty(const QString &name)
{
QtProperty *property = createProperty();
if (property) {
property->setPropertyName(name);
d_ptr->m_properties.insert(property);
initializeProperty(property);
}
return property;
}
2.1.1 QtProperty *property = createProperty(); 我倒!又回到继承类真正创建了该类。
注意 d_ptr->m_propertyToType 的类型: QMap<const QtProperty *, QPair<QtVariantProperty *, int> > m_propertyToType;
QtProperty *QtVariantPropertyManager::createProperty()
{
if (!d_ptr->m_creatingProperty)
return 0;
QtVariantProperty *property = new QtVariantProperty(this);
d_ptr->m_propertyToType.insert(property, qMakePair(property, d_ptr->m_propertyType));
return property;
}
2.1.2 initializeProperty(property); 又回到继承类。这个函数基本歇菜,没看懂
不明白这里为什么要有 d_ptr->m_internalToProperty, 而且管理器都是新创建的:internProp = manager->addProperty();
明白了:这里实际是QtVariantPropertyManager内部保存实际类别管理器的方法。比如 it.value(); 得到的是 QtGroupPropertyManager ,具体的创建就在这里进行。
d_ptr->m_internalToProperty[internProp] 就是用来保存具体的属性的
QtProperty *QtAbstractPropertyManager::addProperty(const QString &name)
{
QtProperty *property = createProperty();
// GROUP 没有重载这个函数,所以这里调用基类创建的属性, return new QtProperty(this);
// 实际上只有基类和QtVariantPropertyManager这2个类有该函数
if (property) {
property->setPropertyName(name);
d_ptr->m_properties.insert(property);
initializeProperty(property);
// 这里GROUP 重载了
// void QtGroupPropertyManager::initializeProperty(QtProperty *property)
// {
// Q_UNUSED(property)
// }
//bool
// void QtBoolPropertyManager::initializeProperty(QtProperty *property)
// {
// d_ptr->m_values[property] = false;
// }
// int
// void QtIntPropertyManager::initializeProperty(QtProperty *property)
// {
// d_ptr->m_values[property] = QtIntPropertyManagerPrivate::Data();
// }
}
return property;
}
void QtVariantPropertyManager::initializeProperty(QtProperty *property)
{
QtVariantProperty *varProp = variantProperty(property);
if (!varProp)
return;
QMap<int, QtAbstractPropertyManager *>::ConstIterator it =
d_ptr->m_typeToPropertyManager.find(d_ptr->m_propertyType);
if (it != d_ptr->m_typeToPropertyManager.constEnd()) {
QtProperty *internProp = 0;
if (!d_ptr->m_creatingSubProperties) {
QtAbstractPropertyManager *manager = it.value();
internProp = manager->addProperty(); // 这里实际的管理器创建内部的具体属性。
d_ptr->m_internalToProperty[internProp] = varProp;
}
propertyToWrappedProperty()->insert(varProp, internProp);
if (internProp) {
QList<QtProperty *> children = internProp->subProperties();
QListIterator<QtProperty *> itChild(children);
QtVariantProperty *lastProperty = 0;
while (itChild.hasNext()) {
QtVariantProperty *prop = d_ptr->createSubProperty(varProp, lastProperty, itChild.next());
lastProperty = prop ? prop : lastProperty;
}
}
}
}
2.1.2.1 QtVariantProperty *varProp = variantProperty(property); 转QtProperty 到QtVariantProperty ,就是一类型转换
QtVariantProperty *QtVariantPropertyManager::variantProperty(const QtProperty *property) const
{
const QMap<const QtProperty *, QPair<QtVariantProperty *, int> >::const_iterator it = d_ptr->m_propertyToType.constFind(property);
if (it == d_ptr->m_propertyToType.constEnd())
return 0;
return it.value().first;
}