上一篇文章简单的介绍了一下QtTreePropertyBrowser树形属性页的使用,这次介绍一下属性页的数据交互。其中的思路借鉴了https://blog.csdn.net/yizhou2010/article/details/52996795这篇文章,感兴趣的可以去围观一下。
h文件:
public:
QMap<QtProperty*, QVariant> m_property_Dic;
private slots:
void ToolTip(QPointF point, bool state);
void keepCallout();
void doPfmDataAnaly();
void doPfmDataDownload();
void doSmoothCurve(bool checked);
void doFittingCurve(bool checked);
void doHideOriginalCurve(bool checked);
void doFitOrderChanged(int index);
void receiveCurveData(int x, int y); //接收传递过来的数据的槽
void variantPropertyValueChanged(QtProperty* property, const QVariant &value);
variantPropertyValueChanged(QtProperty* property, const QVariant &value)函数响应属性值的改变。
cpp文件:
//建立消息映射
connect(m_pParamVarManager, &QtVariantPropertyManager::valueChanged, this, &CDataAnalysisWidget::variantPropertyValueChanged);
//初始化属性树
void CDataAnalysisWidget::InitPropertyTree()
{
m_pParamVarManager = new QtVariantPropertyManager(ui->param_property_tree);
m_pParamVarFactory = new QtVariantEditorFactory(ui->param_property_tree);
//一次调频分析参数
QtProperty *param_group_Item = m_pParamVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("一次调频分析参数"));
QtVariantProperty *param_item = m_pParamVarManager->addProperty(QVariant::Int, QStringLiteral("启动条件(秒)"));
param_item->setValue(10);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 10;
param_item = m_pParamVarManager->addProperty(QVariant::Int, QStringLiteral("结束条件(秒)"));
param_item->setValue(5);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 5;
param_item = m_pParamVarManager->addProperty(QVariant::Double, QStringLiteral("额定功率(MW)"));
param_item->setValue(300);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 300;
param_item = m_pParamVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QStringLiteral("频率阈值(Hz)"));
QStringList enumNames;
enumNames << "0.033Hz" << "0.04Hz" << "0.05Hz";
param_item->setAttribute(QLatin1String("enumNames"), enumNames);
param_item->setValue(1);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 1;
param_item = m_pParamVarManager->addProperty(QVariant::Int, QStringLiteral("最小持续时间(秒)"));
param_item->setValue(15);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 15;
param_item = m_pParamVarManager->addProperty(QVariant::Int, QStringLiteral("计算P0时间长度(秒)"));
param_item->setValue(10);
param_group_Item->addSubProperty(param_item);
m_property_Dic[param_item] = 10;
//曲线参数
QtProperty *fitcurve_group_Item = m_pParamVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), QStringLiteral("曲线参数"));
QtVariantProperty *fitcurve_item = m_pParamVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QStringLiteral("曲线阶数"));
QStringList fittingNames;
for(qint8 iLoop(0); iLoop < 26; iLoop ++)
fittingNames << QString("%1").arg(iLoop + 1);
fitcurve_item->setAttribute(QLatin1String("enumNames"), fittingNames);
fitcurve_item->setValue(15);
fitcurve_group_Item->addSubProperty(fitcurve_item);
ui->param_property_tree->addProperty(param_group_Item);
ui->param_property_tree->addProperty(fitcurve_group_Item);
ui->param_property_tree->setFactoryForManager(m_pParamVarManager, m_pParamVarFactory);
}
//响应函数处理
void CDataAnalysisWidget::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
{
switch (value.type()) {
case QVariant::Int:
m_property_Dic[property] = value.toInt();
break;
case QVariant::Double:
m_property_Dic[property] = value.toDouble();
break;
default:
qDebug() << QString("无此类型");
break;
}
qDebug() << m_property_Dic[property] << value.type();
}
variantPropertyValueChanged()函数找到map里的property,然后根据QVariant的类型更新map里的值。