赞
踩
新建一个qt工程,如图所示
从 D: \Qt\Qt5.6.3\5.6.3\Src\qttools\src\shared 目录下拷贝一份 qtpropertybrowser 源码到刚刚新建的qt工程目录下,如图所示
(注:不一定是 D 盘,根据读者安装qt的路径而定,博主安装的为 qt 5.6.3 )
在工程文件上鼠标右击添加现有文件
添加之后如下所示,将pro文件稍作修改
- #-------------------------------------------------
- #
- # Project created by QtCreator 2023-03-07T17:49:09
- #
- #-------------------------------------------------
-
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- TARGET = Demo
- TEMPLATE = app
-
-
- SOURCES += main.cpp\
- widget.cpp
-
- HEADERS += widget.h
-
- FORMS += widget.ui
-
- DISTFILES += \
- qtpropertybrowser/qtpropertybrowser.pri \
- qtpropertybrowser/qtpropertybrowserutils.pri
-
- RESOURCES += \
- qtpropertybrowser/qtpropertybrowser.qrc
-
修改后
- #-------------------------------------------------
- #
- # Project created by QtCreator 2023-03-07T17:49:09
- #
- #-------------------------------------------------
-
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- #TARGET = Demo
- #TEMPLATE = app
-
- TARGET = qtpropertybrowser
-
- TEMPLATE = lib
-
- CONFIG += staticlib
-
-
- #SOURCES += main.cpp\
- # widget.cpp
-
- #HEADERS += widget.h
-
- #FORMS += widget.ui
-
- include (qtpropertybrowser/qtpropertybrowser.pri)
- include (qtpropertybrowser/qtpropertybrowserutils.pri)
-
- RESOURCES += \
- qtpropertybrowser/qtpropertybrowser.qrc
-
构建>执行qmake>构建项目(ctrl+B)
此时我们可以看到编译生成的debug目录下产生了*.lib文件,如图所示
使用方法请参考用cmake将QHexView编译成静态库这篇文章,下面提供两个测试案例
*.pro文件
- #-------------------------------------------------
- #
- # Project created by QtCreator 2023-03-07T15:54:35
- #
- #-------------------------------------------------
-
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- TARGET = CESHI2
- TEMPLATE = app
-
-
- SOURCES += main.cpp\
- widget.cpp
-
- HEADERS += widget.h
-
- FORMS += widget.ui
-
- win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
-
- INCLUDEPATH += $$PWD/include
- DEPENDPATH += $$PWD/include
-
- win32:!win32-g++: PRE_TARGETDEPS += $$PWD/lib/qtpropertybrowser.lib
- else:win32-g++: PRE_TARGETDEPS += $$PWD/lib/libqtpropertybrowser.a
-
- RESOURCES += \
- src.qrc
*.h文件
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- private:
- Ui::Widget *ui;
- };
-
- #endif // WIDGET_H
*.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include "qtpropertymanager.h"
- #include "qteditorfactory.h"
- #include "qttreepropertybrowser.h"
- #include "qtgroupboxpropertybrowser.h"
- #include "qtbuttonpropertybrowser.h"
-
- #include <QScrollArea>
- #include <QGridLayout>
- #include <QLabel>
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- QtBoolPropertyManager *boolManager = new QtBoolPropertyManager(this);
- QtIntPropertyManager *intManager = new QtIntPropertyManager(this);
- QtStringPropertyManager *stringManager = new QtStringPropertyManager(this);
- QtSizePropertyManager *sizeManager = new QtSizePropertyManager(this);
- QtRectPropertyManager *rectManager = new QtRectPropertyManager(this);
- QtSizePolicyPropertyManager *sizePolicyManager = new QtSizePolicyPropertyManager(this);
- QtEnumPropertyManager *enumManager = new QtEnumPropertyManager(this);
- QtGroupPropertyManager *groupManager = new QtGroupPropertyManager(this);
-
- QtProperty *item0 = groupManager->addProperty("QObject");
-
- QtProperty *item1 = stringManager->addProperty("objectName");
- item0->addSubProperty(item1);
-
- QtProperty *item2 = boolManager->addProperty("enabled");
- item0->addSubProperty(item2);
-
- QtProperty *item3 = rectManager->addProperty("geometry");
- item0->addSubProperty(item3);
-
- QtProperty *item4 = sizePolicyManager->addProperty("sizePolicy");
- item0->addSubProperty(item4);
-
- QtProperty *item5 = sizeManager->addProperty("sizeIncrement");
- item0->addSubProperty(item5);
-
- QtProperty *item7 = boolManager->addProperty("mouseTracking");
- item0->addSubProperty(item7);
-
- QtProperty *item8 = enumManager->addProperty("direction");
- QStringList enumNames;
- enumNames << "Up" << "Right" << "Down" << "Left";
- enumManager->setEnumNames(item8, enumNames);
- QMap<int, QIcon> enumIcons;
- enumIcons[0] = QIcon(":/images/up.png");
- enumIcons[1] = QIcon(":/images/right.png");
- enumIcons[2] = QIcon(":/images/down.png");
- enumIcons[3] = QIcon(":/images/left.png");
- enumManager->setEnumIcons(item8, enumIcons);
- item0->addSubProperty(item8);
-
- QtProperty *item9 = intManager->addProperty("value");
- intManager->setRange(item9, -100, 100);
- item0->addSubProperty(item9);
-
- QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(this);
- QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(this);
- QtSliderFactory *sliderFactory = new QtSliderFactory(this);
- QtScrollBarFactory *scrollBarFactory = new QtScrollBarFactory(this);
- QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
- QtEnumEditorFactory *comboBoxFactory = new QtEnumEditorFactory(this);
-
- QtAbstractPropertyBrowser *editor1 = new QtTreePropertyBrowser();
- editor1->setFactoryForManager(boolManager, checkBoxFactory);
- editor1->setFactoryForManager(intManager, spinBoxFactory);
- editor1->setFactoryForManager(stringManager, lineEditFactory);
- editor1->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
- editor1->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
- editor1->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), spinBoxFactory);
- editor1->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
- editor1->setFactoryForManager(enumManager, comboBoxFactory);
-
- editor1->addProperty(item0);
-
- QtAbstractPropertyBrowser *editor2 = new QtTreePropertyBrowser();
- editor2->addProperty(item0);
-
- QtAbstractPropertyBrowser *editor3 = new QtGroupBoxPropertyBrowser();
- editor3->setFactoryForManager(boolManager, checkBoxFactory);
- editor3->setFactoryForManager(intManager, spinBoxFactory);
- editor3->setFactoryForManager(stringManager, lineEditFactory);
- editor3->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
- editor3->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
- editor3->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), spinBoxFactory);
- editor3->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
- editor3->setFactoryForManager(enumManager, comboBoxFactory);
-
- editor3->addProperty(item0);
-
- QScrollArea *scroll3 = new QScrollArea();
- scroll3->setWidgetResizable(true);
- scroll3->setWidget(editor3);
-
- QtAbstractPropertyBrowser *editor4 = new QtGroupBoxPropertyBrowser();
- editor4->setFactoryForManager(boolManager, checkBoxFactory);
- editor4->setFactoryForManager(intManager, scrollBarFactory);
- editor4->setFactoryForManager(stringManager, lineEditFactory);
- editor4->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
- editor4->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
- editor4->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), sliderFactory);
- editor4->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
- editor4->setFactoryForManager(enumManager, comboBoxFactory);
-
- editor4->addProperty(item0);
-
- QScrollArea *scroll4 = new QScrollArea();
- scroll4->setWidgetResizable(true);
- scroll4->setWidget(editor4);
-
- QtAbstractPropertyBrowser *editor5 = new QtButtonPropertyBrowser();
- editor5->setFactoryForManager(boolManager, checkBoxFactory);
- editor5->setFactoryForManager(intManager, scrollBarFactory);
- editor5->setFactoryForManager(stringManager, lineEditFactory);
- editor5->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
- editor5->setFactoryForManager(rectManager->subIntPropertyManager(), spinBoxFactory);
- editor5->setFactoryForManager(sizePolicyManager->subIntPropertyManager(), sliderFactory);
- editor5->setFactoryForManager(sizePolicyManager->subEnumPropertyManager(), comboBoxFactory);
- editor5->setFactoryForManager(enumManager, comboBoxFactory);
-
- editor5->addProperty(item0);
-
- QScrollArea *scroll5 = new QScrollArea();
- scroll5->setWidgetResizable(true);
- scroll5->setWidget(editor5);
-
- QGridLayout *layout = new QGridLayout(this);
- QLabel *label1 = new QLabel("Editable Tree Property Browser");
- QLabel *label2 = new QLabel("Read Only Tree Property Browser, editor factories are not set");
- QLabel *label3 = new QLabel("Group Box Property Browser");
- QLabel *label4 = new QLabel("Group Box Property Browser with different editor factories");
- QLabel *label5 = new QLabel("Button Property Browser");
- label1->setWordWrap(true);
- label2->setWordWrap(true);
- label3->setWordWrap(true);
- label4->setWordWrap(true);
- label5->setWordWrap(true);
- label1->setFrameShadow(QFrame::Sunken);
- label2->setFrameShadow(QFrame::Sunken);
- label3->setFrameShadow(QFrame::Sunken);
- label4->setFrameShadow(QFrame::Sunken);
- label5->setFrameShadow(QFrame::Sunken);
- label1->setFrameShape(QFrame::Panel);
- label2->setFrameShape(QFrame::Panel);
- label3->setFrameShape(QFrame::Panel);
- label4->setFrameShape(QFrame::Panel);
- label5->setFrameShape(QFrame::Panel);
- label1->setAlignment(Qt::AlignCenter);
- label2->setAlignment(Qt::AlignCenter);
- label3->setAlignment(Qt::AlignCenter);
- label4->setAlignment(Qt::AlignCenter);
- label5->setAlignment(Qt::AlignCenter);
-
- layout->addWidget(label1, 0, 0);
- layout->addWidget(label2, 0, 1);
- layout->addWidget(label3, 0, 2);
- layout->addWidget(label4, 0, 3);
- layout->addWidget(label5, 0, 4);
- layout->addWidget(editor1, 1, 0);
- layout->addWidget(editor2, 1, 1);
- layout->addWidget(scroll3, 1, 2);
- layout->addWidget(scroll4, 1, 3);
- layout->addWidget(scroll5, 1, 4);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
测试结果
*.pro文件
- #-------------------------------------------------
- #
- # Project created by QtCreator 2023-03-07T15:12:32
- #
- #-------------------------------------------------
-
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- TARGET = CeShi
- TEMPLATE = app
-
-
- SOURCES += main.cpp\
- widget.cpp
-
- HEADERS += widget.h
-
- FORMS += widget.ui
-
- win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
-
- INCLUDEPATH += $$PWD/include
- DEPENDPATH += $$PWD/include
*.h文件
- #-------------------------------------------------
- #
- # Project created by QtCreator 2023-03-07T15:12:32
- #
- #-------------------------------------------------
-
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- TARGET = CeShi
- TEMPLATE = app
-
-
- SOURCES += main.cpp\
- widget.cpp
-
- HEADERS += widget.h
-
- FORMS += widget.ui
-
- win32: LIBS += -L$$PWD/lib/ -lqtpropertybrowser
-
- INCLUDEPATH += $$PWD/include
- DEPENDPATH += $$PWD/include
*.cpp文件
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- auto TPBrowser = new QtTreePropertyBrowser(this);
- auto* VPManager = new QtVariantPropertyManager(TPBrowser);
- auto * VEFactory = new QtVariantEditorFactory(TPBrowser);
- TPBrowser->setFactoryForManager(VPManager, VEFactory);
-
- QtProperty *Property = VPManager->addProperty(QtVariantPropertyManager::groupTypeId(),tr("group"));
-
- QtVariantProperty *item = VPManager->addProperty(QVariant::Bool, tr("Bool: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Int, tr("Int: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Double, tr("Double: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::String, tr("String: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Date, tr("Date: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Time, tr("Time: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::DateTime, tr("DateTime: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Locale, tr("Locale: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Rect, tr("Rect: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::RectF, tr("RectF: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Size, tr("Size: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::SizeF, tr("SizeF: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Point, tr("Point: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::PointF, tr("PointF: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Font, tr("Font: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Color, tr("Color: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::Cursor, tr("Cursor: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::KeySequence, tr("KeySequence: "));
- Property->addSubProperty(item);
-
- item = VPManager->addProperty(QVariant::SizePolicy, tr("SizePolicy: "));
- Property->addSubProperty(item);
- TPBrowser->addProperty(Property);
-
- auto layout = new QVBoxLayout();
- layout->addWidget(TPBrowser);
-
- setLayout (layout);
-
- connect (VPManager,&QtVariantPropertyManager::valueChanged,this,&Widget::onValueChanged);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::onValueChanged(QtProperty *property, const QVariant &val)
- {
- qDebug()<<property->propertyName ()<<val;
- }
-
测试结果
打印区
-
- Starting G:\File\qtcode\CeShi-Debug\debug\CeShi.exe...
- "Bool: " QVariant(bool, true)
- "Int: " QVariant(int, 100)
- "String: " QVariant(QString, "1")
- "String: " QVariant(QString, "11")
- "String: " QVariant(QString, "111")
- "String: " QVariant(QString, "1111")
- "Country" QVariant(int, 0)
- "Locale: " QVariant(QLocale, QLocale(C, Default, Default))
- "Language" QVariant(int, 29)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。