1.首先需要安装src
2.pro里添加
include($$[QT_INSTALL_PREFIX]/../Src/qttools/src/shared/qtpropertybrowser/qtpropertybrowser.pri)
3.MainWindow.h
在界面上添加一个widget,然后吧widget提升为QtTreePropertyBrowser,名称为propertyTree
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <qtpropertybrowser.h>
6 #include <qtpropertymanager.h>
7 #include <qtvariantproperty.h>
8
9 namespace Ui {
10 class MainWindow;
11 }
12
13 class MainWindow : public QMainWindow
14 {
15 Q_OBJECT
16
17 public:
18 explicit MainWindow(QWidget *parent = 0);
19 ~MainWindow();
20 public slots:
21 void onValueChanged(QtProperty *property, const QVariant &val);
22 private:
23 Ui::MainWindow *ui;
24 QtVariantPropertyManager *m_pVarManager;
25 QtVariantPropertyManager *m_pVarManager2;
26 QtVariantEditorFactory *m_pVarFactory;
27 QMap<QtProperty*,QString> m_property_dic;
28 };
29
30 #endif // MAINWINDOW_H
4.MainWindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <QDebug>
4
5 MainWindow::MainWindow(QWidget *parent) :
6 QMainWindow(parent),
7 ui(new Ui::MainWindow)
8 {
9 ui->setupUi(this);
10 m_pVarManager = new QtVariantPropertyManager(ui->propertyTree);//关联factory,属性可以修改
11 m_pVarManager2 = new QtVariantPropertyManager(ui->propertyTree);//这个管理器不关联factory,属性不可修改
12 m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
13 connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this, &MainWindow::onValueChanged);//绑定信号槽,当值改变的时候会发送信号
14 QtVariantProperty *groupItem = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("组1"));//添加组
15
16 QtVariantProperty *item = m_pVarManager->addProperty(QVariant::Int,QStringLiteral("整形数据:"));
17 item->setValue(100);
18 groupItem->addSubProperty(item);
19 m_property_dic[item] = QStringLiteral("整形数据:");
20 item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("布尔型数据:"));
21 item->setValue(true);
22 groupItem->addSubProperty(item);
23 m_property_dic[item] = QStringLiteral("布尔型数据:");
24 item = m_pVarManager2->addProperty(QVariant::Bool,QStringLiteral("布尔型数据2:"));
25 item->setValue(true);
26 groupItem->addSubProperty(item);
27 item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("浮点数据:"));
28 item->setValue(3.1415926);
29 groupItem->addSubProperty(item);
30 m_property_dic[item] = QStringLiteral("浮点数据:");
31 item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("字符串数据:"));
32 item->setValue(QStringLiteral("尘中远"));
33 groupItem->addSubProperty(item);
34 m_property_dic[item] = QStringLiteral("字符串数据:");
35 ui->propertyTree->addProperty(groupItem);
36 ui->propertyTree->setFactoryForManager(m_pVarManager,m_pVarFactory);
37 }
38
39 MainWindow::~MainWindow()
40 {
41 delete ui;
42 }
43
44 void MainWindow::onValueChanged(QtProperty *property, const QVariant &value)
45 {
46 QString s =m_property_dic[property];
47 qDebug()<<s<<":"<<value;
48 }
5.效果: