赞
踩
最近在使用Qt做一个项目,用到了类似于qtcreator的UI设计器的树形属性页。借鉴了网上各位大神的帖子接合自己的实现,写点东西留一个脚印。
环境:centos7 + Qt5.10
1、Qt5.10本身已自带QtTreePropertyBrowser,但在Qt5.10安装的时候需勾选“源代码”选项。具体路径:/opt/Qt5.10.1/5.10.1/Src/qttools/src/shared/qtpropertybrowser
2、示例
1)、用qtcreator创建一个“test”工程,打开test.pro文件,加入如下图所示的include一行(红线)
qtpropertybrowser.pri就被包含在test工程中了,也就可以使用qtpropertybrowser了。
2)、有两种方式使用qtpropertybrowser,一种是使用UI设计器,一种是在代码中直接创建对象。下面介绍使用UI设计器的(从MFC过来的,比较倾向于使用设计器,所见即所得)。
打开mainwindow.ui,拉一个widget,右键->提升为->QtTreePropertyBrowser。
接下来就是创建自己的树形属性页了。
具体见下面代码:
mainwindow.h
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~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"
- #include <QDebug>
-
- 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();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。