QT 属性编辑控件 qtpropertybrowser 的用法
1、pro文件中添加
include($$[QT_INSTALL_PREFIX]/../Src/qttools/src/shared/qtpropertybrowser/qtpropertybrowser.pri)
2、ui中添加widget,提升为QtTreePropertyBrowser
3、.h中
#include <qtpropertybrowser.h>
#include <qtpropertymanager.h>
#include <qtvariantproperty.h>
enum PropertyType{BOOL_TYPE, STRING_TYPE, INT_TYPE, DOUBLE_TYPE, ENUM_TYPE};
void AddProperty(PropertyType type, QString propertyName, bool bEditFlag, QString params);
QtVariantPropertyManager *m_pVarMgrEdit;
QtVariantPropertyManager *m_pVarMgrOnlyRead;
QtVariantEditorFactory *m_pVarFactory;
QtVariantProperty *m_groupItem;
QtVariantProperty *m_item;
QMap<QtProperty*,QString> m_property_dic;
4、cpp
void MainWindow::InitAttribute()
{
m_pVarMgrEdit = new QtVariantPropertyManager(ui->propertyTree);//关联factory,属性可以修改
m_pVarMgrOnlyRead = new QtVariantPropertyManager(ui->propertyTree);//这个管理器不关联factory,属性不可修改
m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
connect(m_pVarMgrEdit,&QtVariantPropertyManager::valueChanged,this, &MainWindow::onValueChanged);//绑定信号槽,当值改变的时候会发送信号
//----------------星历/历书----------------
m_groupItem = m_pVarMgrEdit->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("星历/历书"));//添加组
AddProperty(ENUM_TYPE, QStringLiteral("选项"), true, QStringLiteral("使用内置星历;导入历书文件"));
AddProperty(STRING_TYPE, QStringLiteral("可用时间_始(UTC+00:00)"), false, QStringLiteral("2014/07/29 02:30:00"));
AddProperty(STRING_TYPE, QStringLiteral("可用时间_末(UTC+00:00)"), false, QStringLiteral("2014/07/29 04:30:00"));
/*item = m_pVarMgrEdit->addProperty(QVariant::Int, QStringLiteral("Int: "));
item = m_pVarMgrEdit->addProperty(QtVariantPropertyManager::enumTypeId(), QStringLiteral("选项"));
QStringList enumNames;
enumNames << "使用内置星历" << "导入历书文件";
item ->setAttribute(QLatin1String("enumNames"), enumNames);
item ->setValue(0);
m_groupItem->addSubProperty(item );
*/
ui->propertyTree->addProperty(m_groupItem);
//----------------时间设置------------------
m_groupItem = m_pVarMgrEdit->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("时间设置"));//添加组
AddProperty(STRING_TYPE, QStringLiteral("起始时间(UTC+00:00)"), false, QStringLiteral("2014/07/29 02:30:00"));
AddProperty(STRING_TYPE, QStringLiteral("仿真时长"), true, QStringLiteral("60.00分钟"));
ui->propertyTree->addProperty(m_groupItem);
ui->propertyTree->setFactoryForManager(m_pVarMgrEdit,m_pVarFactory);
//将一个工厂与manger关联起来,即可修改内容。
}
void MainWindow::AddProperty(PropertyType type, QString propertyName, bool bEditFlag, QString params)
{
switch (type)
{
case BOOL_TYPE:
{
QVariant tempValue = params;
if(bEditFlag)
{
m_item = m_pVarMgrEdit->addProperty(QVariant::Bool,propertyName);
m_item->setValue(tempValue.toBool());
m_groupItem->addSubProperty(m_item);
}
else
{
m_item = m_pVarMgrOnlyRead->addProperty(QVariant::Bool,propertyName);
m_item->setValue(tempValue.toBool());
m_groupItem->addSubProperty(m_item);
}
break;
}
case DOUBLE_TYPE:
{
if(bEditFlag)
{
m_item = m_pVarMgrEdit->addProperty(QVariant::Double,propertyName);
m_item->setValue(params.toDouble());
m_groupItem->addSubProperty(m_item);
}
else
{
m_item = m_pVarMgrOnlyRead->addProperty(QVariant::Double,propertyName);
m_item->setValue(params.toDouble());
m_groupItem->addSubProperty(m_item);
}
break;
}
case STRING_TYPE:
{
if(bEditFlag)
{
m_item = m_pVarMgrEdit->addProperty(QVariant::String,propertyName);
m_item->setValue(params);
m_groupItem->addSubProperty(m_item);
}
else
{
m_item = m_pVarMgrOnlyRead->addProperty(QVariant::String,propertyName);
m_item->setValue(params);
m_groupItem->addSubProperty(m_item);
}
break;
}
case INT_TYPE:
{
if(bEditFlag)
{
m_item = m_pVarMgrEdit->addProperty(QVariant::Int,propertyName);
m_item->setValue(params.toInt());
m_groupItem->addSubProperty(m_item);
}
else
{
m_item = m_pVarMgrOnlyRead->addProperty(QVariant::Int,propertyName);
m_item->setValue(params.toInt());
m_groupItem->addSubProperty(m_item);
}
break;
}
case ENUM_TYPE:
{
m_item = m_pVarMgrEdit->addProperty(QtVariantPropertyManager::enumTypeId(), propertyName);
QStringList enumNames = params.split(";");
//enumNames << "给定速度加速度" << "圆形轨道" << "弹道轨迹" << "实时仿真数据" << "读取轨迹文件";
m_item ->setAttribute(QLatin1String("enumNames"), enumNames);
m_item ->setValue(0);
m_groupItem->addSubProperty(m_item );
break;
}
default:
break;
}
}
void MainWindow::onValueChanged(QtProperty *property, const QVariant &value)
{
QString s =m_property_dic[property];
qDebug()<<s<<":"<<value;
}