UI property editors for QObject-derived classes.
- QObject editor is tree view similar to the editor in QtDesigner.
- QObjectList editor is a table view where rows are objects and columns are properties.
- Allows dynamic object (row) insertion and deletion similar to Excel.
- Supports object reordering by dragging rows with the mouse.
- Default delegates for editing common value types (these are in addition to the default delegates already in Qt):
- bool: QCheckBox
- QEnum: QComboBox
- double: QLineEdit that can handle scientific notation
- QSize/QSizeF: QLineEdit for text format (w x h)
- QPoint/QPointF: QLineEdit for text format (x, y)
- QRect/QRectF: QLineEdit for text format [(x, y) w x h]
Author: Marcel Goldschen-Ohm
Email: marcel.goldschen@gmail.com
License: MIT
Copyright (c) 2017 Marcel Goldschen-Ohm
Property editor for a QObject is a tree view with two columns of property name/value pairs. Child objects are expandable branches with their own property name/value pairs. Maximum tree depth can be specified (i.e. depth = 0 implies no children shown).
Editor for a list of QObjects is a table where rows are objects and columns are properties. Allows dynamic insertion/deletion of objects (rows) via a context menu obtainable by right clicking on the row headers (similar to Excel). List objects (rows) can be reordered by dragging the row header with the mouse.
Everything is in:
QtPropertyEditor.h
QtPropertyEditor.cpp
The QApplication, same as always.
QApplication app(...);
An object derived from QObject whose properties will be exposed in the editor.
TestObject object; // See test_QtPropertyEditor.h
Children of the object (and their children recursively) are shown as branches of the object's tree.
TestObject *child = new TestObject("MyChild");
child->setParent(&object);
The model interface to our object's properties.
QtPropertyEditor::QtPropertyTreeModel model;
model.setObject(&object);
[Optional] You can define which properties to expose in the editor (default includes all properties including dynamic properties). For example, if we only wanted to show the "objectName" and "myInt" properties:
QList<QByteArray> propertyNames;
propertyNames << "objectName" << "myInt";
model.setPropertyNames(propertyNames);
[Optional] You can map property names to headers that will be displayed instead of the property name. Usually, this is when you want some nonstandard charachters to be displayed that are not allowed to be part of the property name. For example, if we wanted to the "objectName" property to be displayed as if it was the "Name" property instead:
QHash<QByteArray, QString> propertyHeaders;
propertyHeaders["objectName"] = "Name";
model.setPropertyHeaders(propertyHeaders);
The tree view UI editor linked to our object's model interface.
QtPropertyEditor::QtPropertyTreeEditor editor;
editor.setModel(&model);
Show the editor and run the application.
editor.show();
app.exec();
The QApplication, same as always.
QApplication app(...);
A list of objects derived from QObject whose properties will be exposed in the editor. Although it is NOT required, for this example we'll make the objects in our list children of a single parent object.
QObject parent;
for(int i = 0; i < 5; ++i) {
// TestObject defined in test_QtPropertyEditor.h
QObject *object = new TestObject("My Obj " + QString::number(i));
object->setParent(&parent);
}
QObjectList objects = parent->children();
The model interface to the properties in our list of objects.
QtPropertyEditor::QtPropertyTableModel model;
model.setObjects(objects);
[Optional] For dynamic object insertion in the list, you need to supply an object creator function of type QtPropertyTableModel::ObjectCreatorFunction
which is a typedef for std::function<QObject*()>
. If you want the newly created objects to be children of a particular parent object, you need to wrap this into the creator function. For example, as shown below.
// The creator function.
QObject* createNewTestObject(QObject *parent)
{
// TestObject defined in test_QtPropertyEditor.h
return new TestObject("New Test Object", parent);
}
// This will make sure all newly inserted objects
// in the model are children of parent.
std::function<QObject*()> func =
std::bind(createNewTestObject, &parent);
model.setObjectCreator(func);
[Optional] Exposed properties and their column headers can be specified exactly the same as shown in the example above for QtPropertyTreeEditor.
[Optional] Default is a flat editor for each object's properties excluding properties of child objects. However, specific child object properties can be made available in the table view by adding "path.to.child.property" to the specified list of property names to be displayed. In this case, path, to and child are the object names of a child object tree, and property is a property name for child. Note that for this to make sense all objects in the list should have a valid "path.to.child.property". For example, to expose the "myInt" property of the child object named "child":
QList<QByteArray> propertyNames;
propertyNames << "child.myInt";
model.setPropertyNames(propertyNames);
The table view UI editor linked to the model interface for our list of objects.
QtPropertyEditor::QtPropertyTableEditor editor;
editor.setModel(&model);
Show the editor and run the application.
editor.show();
app.exec();