tomwindcloud的专栏

自己的一点记录,非教程,比较乱, 以 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;// IntPropertyManagerQtIntPropertyManager *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)));// DoublePropertyManagerQtDoublePropertyManager *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个类有该函数

一旦有了意志,脚步也会轻松起来。

tomwindcloud的专栏

相关文章:

你感兴趣的文章:

标签云: