赞
踩
Qt安装的时候选择源码,在安装目录下可以看到QtPropertyBrowser的文件。
利用模板,新建一个Qt Widgets Application 的工程。工程名任取,使用qmake构建
这里建议按默认的名称,要不和本博客后文对不上,得自己一个个对应修改。
在Qt工程的pro文件中加入上图中pri 文件的目录,比如我这里是
include(C:/Qt/Qt5.14.2/5.14.2/Src/qttools/src/shared/qtpropertybrowser/qtpropertybrowser.pri)
pri文件相当于一个库的配置文件,指定源码和头文件的位置,编译方式等。这里的作用是利用pri加载QtTreePropertyBrowser库供我们自己的程序使用。
在ui文件中拖入一个Widget控件,我这里命名为:propertyTree,右键promote to(提升为),Promoted class name:QtTreePropertyBrowser
再在界面上添加两个按钮,一个作用是清除属性表,一个是重置属性表为初始值。
mainwindow.h
- #pragma once
-
- #include <QMainWindow>
-
- #include <QDate>
- #include <QLocale>
- #include "qtpropertymanager.h"
- #include "qtvariantproperty.h"
- #include "qttreepropertybrowser.h"
-
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- QtVariantPropertyManager* m_pVarManager;
- QtVariantEditorFactory *m_pVarFactory;
- QMap<QtProperty*, QString> m_property_dic;
-
- private slots:
- void on_clear_pushButton_clicked();
-
- void on_pushButton_2_clicked();
-
- private:
- Ui::MainWindow *ui;
-
- private slots:
- void variantPropertyValueChanged(QtProperty* property, const QVariant &value);
-
-
- };
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
-
- m_pVarManager = new QtVariantPropertyManager(ui->propertyTree);
- m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
-
- connect(m_pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged);
-
- QtProperty *groupItem = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group1"));
-
- QtVariantProperty *item = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
- item->setValue(100);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("Bool: "));
- item->setValue(true);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::Double,QStringLiteral("Double: "));
- item->setValue(3.14);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
- item->setValue(QStringLiteral("hello world"));
- groupItem->addSubProperty(item);
-
- QtProperty *groupItem1 = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group2"));
-
- QtVariantProperty *item1 = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
- item1->setValue(100);
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
- item1->setValue(QStringLiteral("hello world"));
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QVariant::Color, QStringLiteral("Color: "));
- item1->setValue(QColor(255, 0, 0));
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QLatin1String("Enum Property"));
- QStringList enumNames;
- enumNames << "Enum0" << "Enum1" << "Enum2";
- item1->setAttribute(QLatin1String("enumNames"), enumNames);
- item1->setValue(1);
- groupItem1->addSubProperty(item1);
-
- ui->propertyTree->addProperty(groupItem);
- ui->propertyTree->addProperty(groupItem1);
- ui->propertyTree->setFactoryForManager(m_pVarManager, m_pVarFactory);
- }
-
- MainWindow::~MainWindow()
- {
- delete m_pVarManager;
- delete m_pVarFactory;
-
- delete ui;
- }
-
-
- void MainWindow::on_clear_pushButton_clicked()
- {
- m_pVarManager->clear();
- }
-
- void MainWindow::on_pushButton_2_clicked()
- {
- m_pVarManager = new QtVariantPropertyManager(ui->propertyTree);
- m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
-
- connect(m_pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged);
-
- QtProperty *groupItem = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group1"));
-
- QtVariantProperty *item = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
- item->setValue(100);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("Bool: "));
- item->setValue(true);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::Double,QStringLiteral("Double: "));
- item->setValue(3.14);
- groupItem->addSubProperty(item);
-
- item = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
- item->setValue(QStringLiteral("hello world"));
- groupItem->addSubProperty(item);
-
- QtProperty *groupItem1 = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group2"));
-
- QtVariantProperty *item1 = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
- item1->setValue(100);
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
- item1->setValue(QStringLiteral("hello world"));
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QVariant::Color, QStringLiteral("Color: "));
- item1->setValue(QColor(255, 0, 0));
- groupItem1->addSubProperty(item1);
-
- item1 = m_pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QLatin1String("Enum Property"));
- QStringList enumNames;
- enumNames << "Enum0" << "Enum1" << "Enum2";
- item1->setAttribute(QLatin1String("enumNames"), enumNames);
- item1->setValue(1);
- groupItem1->addSubProperty(item1);
-
- ui->propertyTree->addProperty(groupItem);
- ui->propertyTree->addProperty(groupItem1);
- ui->propertyTree->setFactoryForManager(m_pVarManager, m_pVarFactory);
- }
-
-
- void MainWindow::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
- {
- QString str = m_property_dic[property];
-
- // qDebug()<<str<<": "<<value<<": "<<value.type();
- QString str_output=str + ": " + value.toString() + ": " + value.type();
- ui->statusbar->showMessage(str_output, 8000);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。