当前位置:   article > 正文

Qt属性表QtTreePropertyBrowser的添加(编译方式建议用*.Pro)_qt5.14.2 qttreepropertybrowser

qt5.14.2 qttreepropertybrowser

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

  1. #pragma once
  2. #include <QMainWindow>
  3. #include <QDate>
  4. #include <QLocale>
  5. #include "qtpropertymanager.h"
  6. #include "qtvariantproperty.h"
  7. #include "qttreepropertybrowser.h"
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class MainWindow; }
  10. QT_END_NAMESPACE
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14. public:
  15. MainWindow(QWidget *parent = nullptr);
  16. ~MainWindow();
  17. QtVariantPropertyManager* m_pVarManager;
  18. QtVariantEditorFactory *m_pVarFactory;
  19. QMap<QtProperty*, QString> m_property_dic;
  20. private slots:
  21. void on_clear_pushButton_clicked();
  22. void on_pushButton_2_clicked();
  23. private:
  24. Ui::MainWindow *ui;
  25. private slots:
  26. void variantPropertyValueChanged(QtProperty* property, const QVariant &value);
  27. };

mainwindow.cpp 

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

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

闽ICP备14008679号