当前位置:   article > 正文

Qt5.10下QtTreePropertyBrowser的应用_qtvariantpropertymanager

qtvariantpropertymanager

最近在使用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

  1. class MainWindow : public QMainWindow
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit MainWindow(QWidget *parent = 0);
  6. ~MainWindow();
  7. QtVariantPropertyManager* m_pVarManager;
  8. QtVariantEditorFactory *m_pVarFactory;
  9. QMap<QtProperty*, QString> m_property_dic;
  10. private slots:
  11. void on_clear_pushButton_clicked();
  12. void on_pushButton_2_clicked();
  13. private:
  14. Ui::MainWindow *ui;
  15. private slots:
  16. void variantPropertyValueChanged(QtProperty* property, const QVariant &value);
  17. };

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. m_pVarManager = new QtVariantPropertyManager(ui->propertyTree);
  10. m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
  11. connect(m_pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged);
  12. QtProperty *groupItem = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group1"));
  13. QtVariantProperty *item = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
  14. item->setValue(100);
  15. groupItem->addSubProperty(item);
  16. item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("Bool: "));
  17. item->setValue(true);
  18. groupItem->addSubProperty(item);
  19. item = m_pVarManager->addProperty(QVariant::Double,QStringLiteral("Double: "));
  20. item->setValue(3.14);
  21. groupItem->addSubProperty(item);
  22. item = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
  23. item->setValue(QStringLiteral("hello world"));
  24. groupItem->addSubProperty(item);
  25. QtProperty *groupItem1 = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group2"));
  26. QtVariantProperty *item1 = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
  27. item1->setValue(100);
  28. groupItem1->addSubProperty(item1);
  29. item1 = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
  30. item1->setValue(QStringLiteral("hello world"));
  31. groupItem1->addSubProperty(item1);
  32. item1 = m_pVarManager->addProperty(QVariant::Color, QStringLiteral("Color: "));
  33. item1->setValue(QColor(255, 0, 0));
  34. groupItem1->addSubProperty(item1);
  35. item1 = m_pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QLatin1String("Enum Property"));
  36. QStringList enumNames;
  37. enumNames << "Enum0" << "Enum1" << "Enum2";
  38. item1->setAttribute(QLatin1String("enumNames"), enumNames);
  39. item1->setValue(1);
  40. groupItem1->addSubProperty(item1);
  41. ui->propertyTree->addProperty(groupItem);
  42. ui->propertyTree->addProperty(groupItem1);
  43. ui->propertyTree->setFactoryForManager(m_pVarManager, m_pVarFactory);
  44. }
  45. MainWindow::~MainWindow()
  46. {
  47. delete m_pVarManager;
  48. delete m_pVarFactory;
  49. delete ui;
  50. }
  51. void MainWindow::on_clear_pushButton_clicked()
  52. {
  53. m_pVarManager->clear();
  54. }
  55. void MainWindow::on_pushButton_2_clicked()
  56. {
  57. m_pVarManager = new QtVariantPropertyManager(ui->propertyTree);
  58. m_pVarFactory = new QtVariantEditorFactory(ui->propertyTree);
  59. connect(m_pVarManager, &QtVariantPropertyManager::valueChanged, this, &MainWindow::variantPropertyValueChanged);
  60. QtProperty *groupItem = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group1"));
  61. QtVariantProperty *item = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
  62. item->setValue(100);
  63. groupItem->addSubProperty(item);
  64. item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("Bool: "));
  65. item->setValue(true);
  66. groupItem->addSubProperty(item);
  67. item = m_pVarManager->addProperty(QVariant::Double,QStringLiteral("Double: "));
  68. item->setValue(3.14);
  69. groupItem->addSubProperty(item);
  70. item = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
  71. item->setValue(QStringLiteral("hello world"));
  72. groupItem->addSubProperty(item);
  73. QtProperty *groupItem1 = m_pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group2"));
  74. QtVariantProperty *item1 = m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Int: "));
  75. item1->setValue(100);
  76. groupItem1->addSubProperty(item1);
  77. item1 = m_pVarManager->addProperty(QVariant::String,QStringLiteral("String: "));
  78. item1->setValue(QStringLiteral("hello world"));
  79. groupItem1->addSubProperty(item1);
  80. item1 = m_pVarManager->addProperty(QVariant::Color, QStringLiteral("Color: "));
  81. item1->setValue(QColor(255, 0, 0));
  82. groupItem1->addSubProperty(item1);
  83. item1 = m_pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(), QLatin1String("Enum Property"));
  84. QStringList enumNames;
  85. enumNames << "Enum0" << "Enum1" << "Enum2";
  86. item1->setAttribute(QLatin1String("enumNames"), enumNames);
  87. item1->setValue(1);
  88. groupItem1->addSubProperty(item1);
  89. ui->propertyTree->addProperty(groupItem);
  90. ui->propertyTree->addProperty(groupItem1);
  91. ui->propertyTree->setFactoryForManager(m_pVarManager, m_pVarFactory);
  92. }
  93. void MainWindow::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
  94. {
  95. QString str = m_property_dic[property];
  96. qDebug()<<str<<": "<<value<<": "<<value.type();
  97. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/115127
推荐阅读
相关标签
  

闽ICP备14008679号