Qt编程之实现属性窗口编辑器

类似于这种:

Qt编程之实现属性窗口编辑器

就是右下角这个框,有属性名字和对应的value编辑。

这个Widget是作为一个QDockWidget被添加到QMainWindow中的。QMainWindow必须要有centralWidget才能添加DockWidget。DockWidget是可以浮动于centralWidget周边(上下左右)的一种Widget。QMainWindow中有addDockWiget这相应的函数添加。

而QDockWidget中可以设置相应的Widget,这个Widget(实际上就是属性编辑浏览窗口。)这个Widget实际上不是Qt Native自带的。而是Qt的解决方案中提供的Widget,也就是别人帮你封装做好了Widget,拿来用就可以了。这个Widget就是QtPropertyBrowser

样例代码:

nodeproperty.h  此类继承了QDockWidget

 #ifndef NODE_PROPERTY_H
#define NODE_PROPERTY_H #include <QObject>
#include <QDockWidget>
#include "diagramitem.h" QT_BEGIN_NAMESPACE class QDockWidget;
class QtVariantPropertyManager;
class QtProperty;
class QtVariantProperty;
class QtVariantEditorFactory;
class QtTreePropertyBrowser;
QT_END_NAMESPACE class NodePropertyWidget:public QDockWidget
{
public: NodePropertyWidget(DiagramItem::DiagramType type, QWidget* parent = );
~NodePropertyWidget(); private:
void createProcessTreeProperty();
void createDisplayTreeProperty();
void createArrowTreeProperty(); private:
QtVariantPropertyManager *variantManager;
QtProperty *topItem;
QtVariantProperty *item;
QtVariantEditorFactory *variantFactory;
QtTreePropertyBrowser *variantEditor;
}; #endif

nodeproperty.cpp

 #include <QDate>
#include <QLocale>
#include <QDockWidget>
#include <QSizePolicy> #include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "qttreepropertybrowser.h" #include "nodeproperty.h" NodePropertyWidget::NodePropertyWidget(DiagramItem::DiagramType type, QWidget * parent) :QDockWidget(parent)
{ switch (type)
{
case DiagramItem::Process:
setWindowTitle("Process Property");
setFloating(true); setAllowedAreas(Qt::RightDockWidgetArea);
setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
createProcessTreeProperty();
break;
case DiagramItem::Display:
setWindowTitle("Display Property");
setFloating(true); setAllowedAreas(Qt::RightDockWidgetArea);
setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
createDisplayTreeProperty();
break;
case DiagramItem::Line:
setWindowTitle("Arrow Property");
setFloating(true); setAllowedAreas(Qt::RightDockWidgetArea);
setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
createArrowTreeProperty();
break;
default:
break;
}
} NodePropertyWidget::~NodePropertyWidget()
{ delete variantManager;
delete variantFactory;
delete variantEditor;
} void NodePropertyWidget::createProcessTreeProperty(){ variantManager = new QtVariantPropertyManager(); int i = ;
topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
QString::number(i++) + QLatin1String("Process Property")); item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
item->setValue(true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
item->setValue(1.2345);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
item->setValue(1.23456);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
item->setValue("Value");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
item->setValue("Password");
topItem->addSubProperty(item); // Readonly String Property
item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
item->setAttribute(QLatin1String("readOnly"), true);
item->setValue("readonly text");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
item->setValue(QDate::currentDate().addDays());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
item->setValue(QTime::currentTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
item->setValue(QDateTime::currentDateTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
item->setValue(QChar());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
item->setValue(QPoint(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
item->setValue(QPointF(1.2345, -1.23451));
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
item->setValue(QSize(, ));
item->setAttribute(QLatin1String("minimum"), QSize(, ));
item->setAttribute(QLatin1String("maximum"), QSize(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
item->setValue(QSizeF(1.2345, 1.2345));
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
item->setValue(QRect(, , , ));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRect(, , , )); item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRectF(, , , ));
item->setAttribute(QLatin1String("decimals"), ); item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
QString::number(i++) + QLatin1String(" Enum Property"));
QStringList enumNames;
enumNames << "Enum0" << "Enum1" << "Enum2";
item->setAttribute(QLatin1String("enumNames"), enumNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
QString::number(i++) + QLatin1String(" Flag Property"));
QStringList flagNames;
flagNames << "Flag0" << "Flag1" << "Flag2";
item->setAttribute(QLatin1String("flagNames"), flagNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
topItem->addSubProperty(item); variantFactory = new QtVariantEditorFactory(); variantEditor = new QtTreePropertyBrowser();
variantEditor->setFactoryForManager(variantManager, variantFactory);
variantEditor->addProperty(topItem);
variantEditor->setPropertiesWithoutValueMarked(true);
variantEditor->setRootIsDecorated(false); setWidget(variantEditor); //variantEditor->show(); } void NodePropertyWidget::createDisplayTreeProperty(){ variantManager = new QtVariantPropertyManager(); int i = ;
topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
QString::number(i++) + QLatin1String(" Display Property")); item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
item->setValue(true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
item->setValue(1.2345);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
item->setValue(1.23456);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
item->setValue("Value");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
item->setValue("Password");
topItem->addSubProperty(item); // Readonly String Property
item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
item->setAttribute(QLatin1String("readOnly"), true);
item->setValue("readonly text");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
item->setValue(QDate::currentDate().addDays());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
item->setValue(QTime::currentTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
item->setValue(QDateTime::currentDateTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
item->setValue(QChar());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
item->setValue(QPoint(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
item->setValue(QPointF(1.2345, -1.23451));
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
item->setValue(QSize(, ));
item->setAttribute(QLatin1String("minimum"), QSize(, ));
item->setAttribute(QLatin1String("maximum"), QSize(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
item->setValue(QSizeF(1.2345, 1.2345));
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
item->setValue(QRect(, , , ));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRect(, , , )); item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRectF(, , , ));
item->setAttribute(QLatin1String("decimals"), ); item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
QString::number(i++) + QLatin1String(" Enum Property"));
QStringList enumNames;
enumNames << "Enum0" << "Enum1" << "Enum2";
item->setAttribute(QLatin1String("enumNames"), enumNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
QString::number(i++) + QLatin1String(" Flag Property"));
QStringList flagNames;
flagNames << "Flag0" << "Flag1" << "Flag2";
item->setAttribute(QLatin1String("flagNames"), flagNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
topItem->addSubProperty(item); variantFactory = new QtVariantEditorFactory(); variantEditor = new QtTreePropertyBrowser();
variantEditor->setFactoryForManager(variantManager, variantFactory);
variantEditor->addProperty(topItem);
variantEditor->setPropertiesWithoutValueMarked(true);
variantEditor->setRootIsDecorated(false); setWidget(variantEditor); //variantEditor->show(); } void NodePropertyWidget::createArrowTreeProperty(){ variantManager = new QtVariantPropertyManager(); int i = ;
topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
QString::number(i++) + QLatin1String("Arrow Property")); item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
item->setValue(true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
item->setValue();
item->setAttribute(QLatin1String("minimum"), );
item->setAttribute(QLatin1String("maximum"), );
item->setAttribute(QLatin1String("singleStep"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
item->setValue(1.2345);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
item->setValue(1.23456);
item->setAttribute(QLatin1String("singleStep"), 0.1);
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("readOnly"), true);
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
item->setValue("Value");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
item->setValue("Password");
topItem->addSubProperty(item); // Readonly String Property
item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
item->setAttribute(QLatin1String("readOnly"), true);
item->setValue("readonly text");
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
item->setValue(QDate::currentDate().addDays());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
item->setValue(QTime::currentTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
item->setValue(QDateTime::currentDateTime());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
item->setValue(QChar());
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
item->setValue(QPoint(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
item->setValue(QPointF(1.2345, -1.23451));
item->setAttribute(QLatin1String("decimals"), );
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
item->setValue(QSize(, ));
item->setAttribute(QLatin1String("minimum"), QSize(, ));
item->setAttribute(QLatin1String("maximum"), QSize(, ));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
item->setValue(QSizeF(1.2345, 1.2345));
item->setAttribute(QLatin1String("decimals"), );
item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
item->setValue(QRect(, , , ));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRect(, , , )); item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
topItem->addSubProperty(item);
item->setAttribute(QLatin1String("constraint"), QRectF(, , , ));
item->setAttribute(QLatin1String("decimals"), ); item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
QString::number(i++) + QLatin1String(" Enum Property"));
QStringList enumNames;
enumNames << "Enum0" << "Enum1" << "Enum2";
item->setAttribute(QLatin1String("enumNames"), enumNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
QString::number(i++) + QLatin1String(" Flag Property"));
QStringList flagNames;
flagNames << "Flag0" << "Flag1" << "Flag2";
item->setAttribute(QLatin1String("flagNames"), flagNames);
item->setValue();
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
topItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
topItem->addSubProperty(item); variantFactory = new QtVariantEditorFactory(); variantEditor = new QtTreePropertyBrowser();
variantEditor->setFactoryForManager(variantManager, variantFactory);
variantEditor->addProperty(topItem);
variantEditor->setPropertiesWithoutValueMarked(true);
variantEditor->setRootIsDecorated(false); setWidget(variantEditor);
//variantEditor->show(); }

以下的参考中还会给出很多propertybrowser的很多例子

references:

https://github.com/commontk/QtPropertyBrowser

上一篇:boost asio io_service学习笔记


下一篇:CenterOS下安装Nginx