控件能加載拖曳拉伸了,這些都是基本的前提工做,接下來的重點就是要動態加載選中控件的屬性了,Qt的屬性機制那是異常的強大,只能用強大到爆來形容,Qt中編寫自定義控件,若是屬性都用Q_PROPERTY來描述過,那都支持整個Qt生態環境中的各類應用場景的加載,好比widget的屬性設計,qml中的屬性識別等,都是很是的方便。 仔細觀察QtDesigner設計器的屬性控件,看起來和表格控件很相似,若是本身從新造輪子整一個,須要花費巨大的代價,其實Qt已經有這個開源組件在qt-solutions-master中,名字叫qtpropertybrowser,搜索下有不少該組件的使用方法,demo也是很是的詳細,本人改過其中的部分代碼以便過濾父類屬性和中文屬性映射等。 體驗地址:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取碼:877p 文件:可執行文件.ziplinux
void QtObjectController::setObject(QObject *object) { //若是設置的控件已是當前控件則不處理 if (d_ptr->m_object == object) { return; } if (d_ptr->m_object) { d_ptr->saveExpandedState(); QListIterator<QtProperty *> it(d_ptr->m_topLevelProperties); while (it.hasNext()) { d_ptr->m_browser->removeProperty(it.next()); } d_ptr->m_topLevelProperties.clear(); } d_ptr->m_object = object; if (!d_ptr->m_object) { return; } //加載父類的屬性 d_ptr->addClassPropertiesParent(d_ptr->m_object->metaObject()->superClass()); //加載當前控件的屬性 d_ptr->addClassProperties(d_ptr->m_object->metaObject()); //存儲節點狀態 d_ptr->restoreExpandedState(); //摺疊全部節點 d_ptr->collapseAll(); } void QtObjectControllerPrivate::addClassProperties(const QMetaObject *metaObject) { if (!metaObject) { return; } QtProperty *classProperty = m_classToProperty.value(metaObject); if (!classProperty) { QString className = QLatin1String(metaObject->className()); classProperty = m_manager->addProperty(QtVariantPropertyManager::groupTypeId(), className); m_classToProperty[metaObject] = classProperty; m_propertyToClass[classProperty] = metaObject; for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++) { QMetaProperty metaProperty = metaObject->property(idx); int type = metaProperty.userType(); QtVariantProperty *subProperty = 0; //將英文屬性換成中文屬性 QString propertyName = metaProperty.name(); propertyName = QtPropertyName::maps.value(propertyName, propertyName); if (!metaProperty.isReadable()) { subProperty = m_readOnlyManager->addProperty(QVariant::String, propertyName); subProperty->setValue(QLatin1String("< Non Readable >")); } else if (metaProperty.isEnumType()) { if (metaProperty.isFlagType()) { subProperty = m_manager->addProperty(QtVariantPropertyManager::flagTypeId(), propertyName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; QStringList flagNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value) && isPowerOf2(value)) { valueMap[value] = true; flagNames.append(QLatin1String(metaEnum.key(i))); } subProperty->setAttribute(QLatin1String("flagNames"), flagNames); subProperty->setValue(flagToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else { subProperty = m_manager->addProperty(QtVariantPropertyManager::enumTypeId(), propertyName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; // dont show multiple enum values which have the same values QStringList enumNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value)) { valueMap[value] = true; //將枚舉類型強制轉爲中文 QString enumName = metaEnum.key(i); enumName = QtPropertyName::maps.value(enumName, enumName); enumNames.append(enumName); } } subProperty->setAttribute(QLatin1String("enumNames"), enumNames); subProperty->setValue(enumToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else if (m_manager->isPropertyTypeSupported(type)) { if (!metaProperty.isWritable()) { subProperty = m_readOnlyManager->addProperty(type, propertyName + QLatin1String(" (Non Writable)")); } if (!metaProperty.isDesignable()) { subProperty = m_readOnlyManager->addProperty(type, propertyName + QLatin1String(" (Non Designable)")); } else { subProperty = m_manager->addProperty(type, propertyName); } subProperty->setValue(metaProperty.read(m_object)); } else { subProperty = m_readOnlyManager->addProperty(QVariant::String, propertyName); subProperty->setValue(QLatin1String("< Unknown Type >")); subProperty->setEnabled(false); } classProperty->addSubProperty(subProperty); m_propertyToIndex[subProperty] = idx; m_classToIndexToProperty[metaObject][idx] = subProperty; } } else { updateClassProperties(metaObject, false); } m_topLevelProperties.append(classProperty); m_browser->addProperty(classProperty); } void QtObjectControllerPrivate::addClassPropertiesParent(const QMetaObject *metaObject) { if (!metaObject) { return; } //存儲須要過濾的屬性,有時候大部分屬性都用不上 QStringList keyName; keyName << "geometry"; QtProperty *classProperty = m_classToProperty.value(metaObject); if (!classProperty) { QString className = QLatin1String(metaObject->className()); classProperty = m_manager->addProperty(QtVariantPropertyManager::groupTypeId(), className); m_classToProperty[metaObject] = classProperty; m_propertyToClass[classProperty] = metaObject; for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++) { QMetaProperty metaProperty = metaObject->property(idx); int type = metaProperty.userType(); QtVariantProperty *subProperty = 0; //若是當前屬性不在須要過濾的屬性中則繼續下一個屬性判斷 QString propertyName = metaProperty.name(); if (!keyName.contains(propertyName)) { continue; } propertyName = QtPropertyName::maps.value(propertyName, propertyName); if (!metaProperty.isReadable()) { subProperty = m_readOnlyManager->addProperty(QVariant::String, propertyName); subProperty->setValue(QLatin1String("< Non Readable >")); } else if (metaProperty.isEnumType()) { if (metaProperty.isFlagType()) { subProperty = m_manager->addProperty(QtVariantPropertyManager::flagTypeId(), propertyName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; QStringList flagNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value) && isPowerOf2(value)) { valueMap[value] = true; flagNames.append(QLatin1String(metaEnum.key(i))); } subProperty->setAttribute(QLatin1String("flagNames"), flagNames); subProperty->setValue(flagToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else { subProperty = m_manager->addProperty(QtVariantPropertyManager::enumTypeId(), propertyName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; // dont show multiple enum values which have the same values QStringList enumNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value)) { valueMap[value] = true; enumNames.append(QLatin1String(metaEnum.key(i))); } } subProperty->setAttribute(QLatin1String("enumNames"), enumNames); subProperty->setValue(enumToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else if (m_manager->isPropertyTypeSupported(type)) { if (!metaProperty.isWritable()) { subProperty = m_readOnlyManager->addProperty(type, propertyName + QLatin1String(" (Non Writable)")); } if (!metaProperty.isDesignable()) { subProperty = m_readOnlyManager->addProperty(type, propertyName + QLatin1String(" (Non Designable)")); } else { subProperty = m_manager->addProperty(type, propertyName); } subProperty->setValue(metaProperty.read(m_object)); } else { subProperty = m_readOnlyManager->addProperty(QVariant::String, propertyName); subProperty->setValue(QLatin1String("< Unknown Type >")); subProperty->setEnabled(false); } classProperty->addSubProperty(subProperty); m_propertyToIndex[subProperty] = idx; m_classToIndexToProperty[metaObject][idx] = subProperty; } } else { updateClassProperties(metaObject, false); } m_topLevelProperties.append(classProperty); m_browser->addProperty(classProperty); }