当前位置:   article > 正文

qtpropertybrowser编译成静态库_qt 生成 静态库

qt 生成 静态库

一、新建一个qt工程

新建一个qt工程,如图所示

二、拷贝qtpropertybrowser源码

D: \Qt\Qt5.6.3\5.6.3\Src\qttools\src\shared 目录下拷贝一份 qtpropertybrowser 源码到刚刚新建的qt工程目录下,如图所示
(注:不一定是 D 盘,根据读者安装qt的路径而定,博主安装的为 qt 5.6.3

三、添加源文件

在工程文件上鼠标右击添加现有文件
添加之后如下所示,将pro文件稍作修改
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2023-03-07T17:49:09
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = Demo
  9. TEMPLATE = app
  10. SOURCES += main.cpp\
  11. widget.cpp
  12. HEADERS += widget.h
  13. FORMS += widget.ui
  14. DISTFILES += \
  15. qtpropertybrowser/qtpropertybrowser.pri \
  16. qtpropertybrowser/qtpropertybrowserutils.pri
  17. RESOURCES += \
  18. qtpropertybrowser/qtpropertybrowser.qrc

修改后

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2023-03-07T17:49:09
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. #TARGET = Demo
  9. #TEMPLATE = app
  10. TARGET = qtpropertybrowser
  11. TEMPLATE = lib
  12. CONFIG += staticlib
  13. #SOURCES += main.cpp\
  14. # widget.cpp
  15. #HEADERS += widget.h
  16. #FORMS += widget.ui
  17. include (qtpropertybrowser/qtpropertybrowser.pri)
  18. include (qtpropertybrowser/qtpropertybrowserutils.pri)
  19. RESOURCES += \
  20. qtpropertybrowser/qtpropertybrowser.qrc

四、编译生成静态库

构建>执行qmake>构建项目(ctrl+B)

此时我们可以看到编译生成的debug目录下产生了*.lib文件,如图所示

使用方法请参考用cmake将QHexView编译成静态库这篇文章,下面提供两个测试案例

五、qtpropertybrowser测试代码

案例一

*.pro文件

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2023-03-07T15:54:35
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = CESHI2
  9. TEMPLATE = app
  10. SOURCES += main.cpp\
  11. widget.cpp
  12. HEADERS += widget.h
  13. FORMS += widget.ui
  14. win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
  15. INCLUDEPATH += $$PWD/include
  16. DEPENDPATH += $$PWD/include
  17. win32:!win32-g++: PRE_TARGETDEPS += $$PWD/lib/qtpropertybrowser.lib
  18. else:win32-g++: PRE_TARGETDEPS += $$PWD/lib/libqtpropertybrowser.a
  19. RESOURCES += \
  20. src.qrc

*.h文件

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. namespace Ui {
  5. class Widget;
  6. }
  7. class Widget : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit Widget(QWidget *parent = 0);
  12. ~Widget();
  13. private:
  14. Ui::Widget *ui;
  15. };
  16. #endif // WIDGET_H

*.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include "qtpropertymanager.h"
  4. #include "qteditorfactory.h"
  5. #include "qttreepropertybrowser.h"
  6. #include "qtgroupboxpropertybrowser.h"
  7. #include "qtbuttonpropertybrowser.h"
  8. #include <QScrollArea>
  9. #include <QGridLayout>
  10. #include <QLabel>
  11. Widget::Widget(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::Widget)
  14. {
  15. ui->setupUi(this);
  16. QtBoolPropertyManager *boolManager = new QtBoolPropertyManager(this);
  17. QtIntPropertyManager *intManager = new QtIntPropertyManager(this);
  18. QtStringPropertyManager *stringManager = new QtStringPropertyManager(this);
  19. QtSizePropertyManager *sizeManager = new QtSizePropertyManager(this);
  20. QtRectPropertyManager *rectManager = new QtRectPropertyManager(this);
  21. QtSizePolicyPropertyManager *sizePolicyManager = new QtSizePolicyPropertyManager(this);
  22. QtEnumPropertyManager *enumManager = new QtEnumPropertyManager(this);
  23. QtGroupPropertyManager *groupManager = new QtGroupPropertyManager(this);
  24. QtProperty *item0 = groupManager->addProperty("QObject");
  25. QtProperty *item1 = stringManager->addProperty("objectName");
  26. item0->addSubProperty(item1);
  27. QtProperty *item2 = boolManager->addProperty("enabled");
  28. item0->addSubProperty(item2);
  29. QtProperty *item3 = rectManager->addProperty("geometry");
  30. item0->addSubProperty(item3);
  31. QtProperty *item4 = sizePolicyManager->addProperty("sizePolicy");
  32. item0->addSubProperty(item4);
  33. QtProperty *item5 = sizeManager->addProperty("sizeIncrement");
  34. item0->addSubProperty(item5);
  35. QtProperty *item7 = boolManager->addProperty("mouseTracking");
  36. item0->addSubProperty(item7);
  37. QtProperty *item8 = enumManager->addProperty("direction");
  38. QStringList enumNames;
  39. enumNames << "Up" << "Right" << "Down" << "Left";
  40. enumManager->setEnumNames(item8, enumNames);
  41. QMap<int, QIcon> enumIcons;
  42. enumIcons[0] = QIcon(":/images/up.png");
  43. enumIcons[1] = QIcon(":/images/right.png");
  44. enumIcons[2] = QIcon(":/images/down.png");
  45. enumIcons[3] = QIcon(":/images/left.png");
  46. enumManager->setEnumIcons(item8, enumIcons);
  47. item0->addSubProperty(item8);
  48. QtProperty *item9 = intManager->addProperty("value");
  49. intManager->setRange(item9, -100, 100);
  50. item0->addSubProperty(item9);
  51. QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(this);
  52. QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(this);
  53. QtSliderFactory *sliderFactory = new QtSliderFactory(this);
  54. QtScrollBarFactory *scrollBarFactory = new QtScrollBarFactory(this);
  55. QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
  56. QtEnumEditorFactory *comboBoxFactory = new QtEnumEditorFactory(this);
  57. QtAbstractPropertyBrowser *editor1 = new QtTreePropertyBrowser();
  58. editor1->setFactoryForManager(boolManager, checkBoxFactory);
  59. editor1->setFactoryForManager(intManager, spinBoxFactory);
  60. editor1->setFactoryForManager(stringManager, lineEditFactory);
  61. editor1->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
  62. editor1->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
  63. editor1->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), spinBoxFactory);
  64. editor1->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
  65. editor1->setFactoryForManager(enumManager, comboBoxFactory);
  66. editor1->addProperty(item0);
  67. QtAbstractPropertyBrowser *editor2 = new QtTreePropertyBrowser();
  68. editor2->addProperty(item0);
  69. QtAbstractPropertyBrowser *editor3 = new QtGroupBoxPropertyBrowser();
  70. editor3->setFactoryForManager(boolManager, checkBoxFactory);
  71. editor3->setFactoryForManager(intManager, spinBoxFactory);
  72. editor3->setFactoryForManager(stringManager, lineEditFactory);
  73. editor3->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
  74. editor3->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
  75. editor3->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), spinBoxFactory);
  76. editor3->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
  77. editor3->setFactoryForManager(enumManager, comboBoxFactory);
  78. editor3->addProperty(item0);
  79. QScrollArea *scroll3 = new QScrollArea();
  80. scroll3->setWidgetResizable(true);
  81. scroll3->setWidget(editor3);
  82. QtAbstractPropertyBrowser *editor4 = new QtGroupBoxPropertyBrowser();
  83. editor4->setFactoryForManager(boolManager, checkBoxFactory);
  84. editor4->setFactoryForManager(intManager, scrollBarFactory);
  85. editor4->setFactoryForManager(stringManager, lineEditFactory);
  86. editor4->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
  87. editor4->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
  88. editor4->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), sliderFactory);
  89. editor4->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
  90. editor4->setFactoryForManager(enumManager, comboBoxFactory);
  91. editor4->addProperty(item0);
  92. QScrollArea *scroll4 = new QScrollArea();
  93. scroll4->setWidgetResizable(true);
  94. scroll4->setWidget(editor4);
  95. QtAbstractPropertyBrowser *editor5 = new QtButtonPropertyBrowser();
  96. editor5->setFactoryForManager(boolManager, checkBoxFactory);
  97. editor5->setFactoryForManager(intManager, scrollBarFactory);
  98. editor5->setFactoryForManager(stringManager, lineEditFactory);
  99. editor5->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
  100. editor5->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
  101. editor5->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), sliderFactory);
  102. editor5->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
  103. editor5->setFactoryForManager(enumManager, comboBoxFactory);
  104. editor5->addProperty(item0);
  105. QScrollArea *scroll5 = new QScrollArea();
  106. scroll5->setWidgetResizable(true);
  107. scroll5->setWidget(editor5);
  108. QGridLayout *layout = new QGridLayout(this);
  109. QLabel *label1 = new QLabel("Editable Tree Property Browser");
  110. QLabel *label2 = new QLabel("Read Only Tree Property Browser, editor factories are not set");
  111. QLabel *label3 = new QLabel("Group Box Property Browser");
  112. QLabel *label4 = new QLabel("Group Box Property Browser with different editor factories");
  113. QLabel *label5 = new QLabel("Button Property Browser");
  114. label1->setWordWrap(true);
  115. label2->setWordWrap(true);
  116. label3->setWordWrap(true);
  117. label4->setWordWrap(true);
  118. label5->setWordWrap(true);
  119. label1->setFrameShadow(QFrame::Sunken);
  120. label2->setFrameShadow(QFrame::Sunken);
  121. label3->setFrameShadow(QFrame::Sunken);
  122. label4->setFrameShadow(QFrame::Sunken);
  123. label5->setFrameShadow(QFrame::Sunken);
  124. label1->setFrameShape(QFrame::Panel);
  125. label2->setFrameShape(QFrame::Panel);
  126. label3->setFrameShape(QFrame::Panel);
  127. label4->setFrameShape(QFrame::Panel);
  128. label5->setFrameShape(QFrame::Panel);
  129. label1->setAlignment(Qt::AlignCenter);
  130. label2->setAlignment(Qt::AlignCenter);
  131. label3->setAlignment(Qt::AlignCenter);
  132. label4->setAlignment(Qt::AlignCenter);
  133. label5->setAlignment(Qt::AlignCenter);
  134. layout->addWidget(label1, 0, 0);
  135. layout->addWidget(label2, 0, 1);
  136. layout->addWidget(label3, 0, 2);
  137. layout->addWidget(label4, 0, 3);
  138. layout->addWidget(label5, 0, 4);
  139. layout->addWidget(editor1, 1, 0);
  140. layout->addWidget(editor2, 1, 1);
  141. layout->addWidget(scroll3, 1, 2);
  142. layout->addWidget(scroll4, 1, 3);
  143. layout->addWidget(scroll5, 1, 4);
  144. }
  145. Widget::~Widget()
  146. {
  147. delete ui;
  148. }

测试结果

案例二

*.pro文件

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2023-03-07T15:12:32
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = CeShi
  9. TEMPLATE = app
  10. SOURCES += main.cpp\
  11. widget.cpp
  12. HEADERS += widget.h
  13. FORMS += widget.ui
  14. win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
  15. INCLUDEPATH += $$PWD/include
  16. DEPENDPATH += $$PWD/include

*.h文件

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2023-03-07T15:12:32
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = CeShi
  9. TEMPLATE = app
  10. SOURCES += main.cpp\
  11. widget.cpp
  12. HEADERS += widget.h
  13. FORMS += widget.ui
  14. win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
  15. INCLUDEPATH += $$PWD/include
  16. DEPENDPATH += $$PWD/include

*.cpp文件

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::Widget)
  6. {
  7. ui->setupUi(this);
  8. auto TPBrowser = new QtTreePropertyBrowser(this);
  9. auto* VPManager = new QtVariantPropertyManager(TPBrowser);
  10. auto * VEFactory = new QtVariantEditorFactory(TPBrowser);
  11. TPBrowser->setFactoryForManager(VPManager, VEFactory);
  12. QtProperty *Property = VPManager->addProperty(QtVariantPropertyManager::groupTypeId(),tr("group"));
  13. QtVariantProperty *item = VPManager->addProperty(QVariant::Bool, tr("Bool: "));
  14. Property->addSubProperty(item);
  15. item = VPManager->addProperty(QVariant::Int, tr("Int: "));
  16. Property->addSubProperty(item);
  17. item = VPManager->addProperty(QVariant::Double, tr("Double: "));
  18. Property->addSubProperty(item);
  19. item = VPManager->addProperty(QVariant::String, tr("String: "));
  20. Property->addSubProperty(item);
  21. item = VPManager->addProperty(QVariant::Date, tr("Date: "));
  22. Property->addSubProperty(item);
  23. item = VPManager->addProperty(QVariant::Time, tr("Time: "));
  24. Property->addSubProperty(item);
  25. item = VPManager->addProperty(QVariant::DateTime, tr("DateTime: "));
  26. Property->addSubProperty(item);
  27. item = VPManager->addProperty(QVariant::Locale, tr("Locale: "));
  28. Property->addSubProperty(item);
  29. item = VPManager->addProperty(QVariant::Rect, tr("Rect: "));
  30. Property->addSubProperty(item);
  31. item = VPManager->addProperty(QVariant::RectF, tr("RectF: "));
  32. Property->addSubProperty(item);
  33. item = VPManager->addProperty(QVariant::Size, tr("Size: "));
  34. Property->addSubProperty(item);
  35. item = VPManager->addProperty(QVariant::SizeF, tr("SizeF: "));
  36. Property->addSubProperty(item);
  37. item = VPManager->addProperty(QVariant::Point, tr("Point: "));
  38. Property->addSubProperty(item);
  39. item = VPManager->addProperty(QVariant::PointF, tr("PointF: "));
  40. Property->addSubProperty(item);
  41. item = VPManager->addProperty(QVariant::Font, tr("Font: "));
  42. Property->addSubProperty(item);
  43. item = VPManager->addProperty(QVariant::Color, tr("Color: "));
  44. Property->addSubProperty(item);
  45. item = VPManager->addProperty(QVariant::Cursor, tr("Cursor: "));
  46. Property->addSubProperty(item);
  47. item = VPManager->addProperty(QVariant::KeySequence, tr("KeySequence: "));
  48. Property->addSubProperty(item);
  49. item = VPManager->addProperty(QVariant::SizePolicy, tr("SizePolicy: "));
  50. Property->addSubProperty(item);
  51. TPBrowser->addProperty(Property);
  52. auto layout = new QVBoxLayout();
  53. layout->addWidget(TPBrowser);
  54. setLayout (layout);
  55. connect (VPManager,&QtVariantPropertyManager::valueChanged,this,&Widget::onValueChanged);
  56. }
  57. Widget::~Widget()
  58. {
  59. delete ui;
  60. }
  61. void Widget::onValueChanged(QtProperty *property, const QVariant &val)
  62. {
  63. qDebug()<<property->propertyName ()<<val;
  64. }

测试结果

打印区

  1. Starting G:\File\qtcode\CeShi-Debug\debug\CeShi.exe...
  2. "Bool: " QVariant(bool, true)
  3. "Int: " QVariant(int, 100)
  4. "String: " QVariant(QString, "1")
  5. "String: " QVariant(QString, "11")
  6. "String: " QVariant(QString, "111")
  7. "String: " QVariant(QString, "1111")
  8. "Country" QVariant(int, 0)
  9. "Locale: " QVariant(QLocale, QLocale(C, Default, Default))
  10. "Language" QVariant(int, 29)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/115122?site
推荐阅读
相关标签
  

闽ICP备14008679号