Qt属性表控件的使用 QtTreePropertyBrowser
属性表是vs2003时引入的的新控件,用于流量和设置大量的信息,现在,很多软件上都能看到它的身影,如vs,Qt Creator等IDE的详细设置里都离不开属性表。
2.安装部署
想用动态链接库的,只需要自己新建一个config.pri文件,里面写上SOLUTIONS_LIBRARY = yes即可。建议大家使用动态库,避免一些moc文件生成的一些诡异问题~~
部署QtTreePropertyBrowser非常简单,只需要把解压的文件夹放在你的工程目录下,在pro文件里加入
3.属性表的使用简介
3.1添加内容
-
QtBoolPropertyManager
-
QtColorPropertyManager
-
QtDatePropertyManager
-
QtDateTimePropertyManager
-
QtDoublePropertyManager
-
QtEnumPropertyManager
-
QtFlagPropertyManager
-
QtFontPropertyManager
-
QtGroupPropertyManager
-
QtIntPropertyManager
-
QtPointPropertyManager
-
QtRectPropertyManager
-
QtSizePropertyManager
-
QtSizePolicyPropertyManager
-
QtStringPropertyManager
-
QtTimePropertyManager
-
QtVariantPropertyManager
- QtVariantPropertyManager*m_pVarManager;
- m_pVarManager =newQtVariantPropertyManager(ui->propertyTree);
- QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("整形数据:"));
- item->setValue(101);
- ui->propertyTree->addProperty(item);
- item =m_pVarManager->addProperty(QVariant::Bool, QStringLiteral("布尔型数据:"));
- item->setValue(true);
- ui->propertyTree->addProperty(item);
- item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("浮点数据:"));
- item->setValue(3.1415926);
- ui->propertyTree->addProperty(item);
- item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("字符串数据:"));
- ui->propertyTree->addProperty(item);
- item->setValue(QStringLiteral("尘中远"));
效果如下:
- m_pVarManager =newQtVariantPropertyManager(ui->propertyTree);
- QtProperty *groupItem =m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("组1"));
- QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("整形数据:"));
- item->setValue(101);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::Bool, QStringLiteral("布尔型数据:"));
- item->setValue(true);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("浮点数据:"));
- item->setValue(3.1415926);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("字符串数据:"));
- groupItem->addSubProperty(item);
- item->setValue(QStringLiteral("尘中远"));
- ui->propertyTree->addProperty(groupItem);
- QtVariantPropertyManager*m_pVarManager;
- QtVariantEditorFactory*m_pVarFactory;
cpp
- m_pVarManager =newQtVariantPropertyManager(ui->propertyTree);
- m_pVarFactory =newQtVariantEditorFactory(ui->propertyTree);
- QtProperty *groupItem =m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("组1"));
- QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("整形数据:"));
- item->setValue(101);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::Bool, QStringLiteral("布尔型数据:"));
- item->setValue(true);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("浮点数据:"));
- item->setValue(3.1415926);
- groupItem->addSubProperty(item);
- item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("字符串数据:"));
- groupItem->addSubProperty(item);
- item->setValue(QStringLiteral("尘中远"));
- ui->propertyTree->addProperty(groupItem);
- ui->propertyTree->setFactoryForManager(m_pVarManager,m_pVarFactory);
void QtAbstractPropertyManager::propertyChanged ( QtProperty * property );
如果是用QtVariantPropertyManager,那么它的valueChanged 信号将会是你经常使用的信号,它会发生发生更改的属性指针以及更改后的值。
void valueChanged ( QtProperty * property, const QVariant & value )
但为了知道是哪个属性,需要附加一个map来进行记录,
h文件
- QMap<QtProperty*,QString>m_property_dic;
- QtVariantPropertyManager*m_pVarManager;
- QtVariantEditorFactory*m_pVarFactory;
cpp
- XXXClass::XXXClass(QWidget *parent)
- :QMainWindow(parent),ui(newUi::XXXClass) {
- m_pVarManager =newQtVariantPropertyManager(ui->propertyTree);
- m_pVarFactory =newQtVariantEditorFactory(ui->propertyTree);
- connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this,&XXXClass::variantPropertyValueChanged);
- ui->propertyTree->setFactoryForManager(m_pVarManager,m_pVarFactory);
- QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("参数1"));
- item->setValue(1);
- ui->propertyTree->addProperty(item);
- m_property_dic[item] =QStringLiteral("参数1");
- item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("参数2"));
- item->setValue(2);
- ui->propertyTree->addProperty(item);
- m_property_dic[item] =QStringLiteral("参数2");
- item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("参数3"));
- item->setValue(QStringLiteral("czy"));
- ui->propertyTree->addProperty(item);
- m_property_dic[item] =QStringLiteral("参数3");
- item =m_pVarManager->addProperty(QVariant::Bool, QStringLiteral("参数4"));
- item->setValue(true);
- ui->propertyTree->addProperty(item);
- m_property_dic[item] =QStringLiteral("参数4");
- }
- voidXXXClass::variantPropertyValueChanged(QtProperty *property, constQVariant &value)
- {
- QString s =m_property_dic[property];
- qDebug()<<s<<":"<<value;
- }
输出:
"" : QVariant(int, 1)
"" : QVariant(double, 2)
"" : QVariant(QString, "czy")
"" : QVariant(bool, true)
"参数1" : QVariant(int, 123)
"参数2" : QVariant(double, 13)
"参数3" : QVariant(QString, "")
"参数3" : QVariant(QString, "中国")
"参数4" : QVariant(bool, false)
"参数4" : QVariant(bool, true)
参考提供的例子,有时候,为了方便双向控制,还会建立一个映射
QMap<QtProperty *, QString> propertyToId;
QMap<QString, QtProperty *> idToProperty;