Using QtPropertyBrowser as an Advanced Configuration Editor

Has anyone used QtPropertyBrowser as an advanced configuration editor? All that I see in the GUI examples is for editing the properties of GUI elements. But how do I get started if I would like to edit something more abstract, for example, the configuration of the application.

Here is an example:

I am creating an application with a multi-page configuration dialog. Some parameters relate to the printer, some relate to the database, some refer to general application parameters, and some refer to template file names and / or templates. But before creating all these detailed configuration pages in the editor, I would like to have the “advanced” or “expert” tab in the dialog box that lists all the possible configuration options. Later in the design process, I collected more and more options, deciding which “lightweight” options could be added to more user-friendly configuration pages. And googling around I came through QtPropertyBrowser, which seems to be the right tool. But I'm not sure where to start? I'm sure,that instead of GUI objects, I need some abstract configuration objects (one or more). But I do not know where and how to start from this. Currently, all my ideas look complicated.

Any suggestions or pointers to tips?

+3
source share
2 answers

You might want to take a look at the runtime type information available through the class QMetaObject. Your data objects must be QObjectdescendants and declared as macros QObject. You will also need a simple procedure that will iterate over the properties of data objects and create and configure the corresponding properties of the editor. The meta object also provides an interface for dumping values ​​and calling methods. Further information on the Qt property system is here: Property System . Below is a small example of how you could do this:

:

QtTreePropertyBrowser       *_browser;
QtIntPropertyManager        *_intManager;
QtDoublePropertyManager     *_doubleManager;
QtStringPropertyManager     *_stringManager;

_intManager = new QtIntPropertyManager();
_doubleManager = new QtDoublePropertyManager();
_stringManager = new QtStringPropertyManager();

_browser = new QtTreePropertyBrowser(ui->centralWidget);

:

void loadProperties(QObject *object)
{
    _browser->clear();
    if (object)
    {
        const QMetaObject *meta = object->metaObject();

        qDebug() << "class : " << meta->className();

        for (int i=0; i<meta->propertyCount(); i++)
        {
            QMetaProperty   metaProperty = meta->property(i);
            QVariant        value = metaProperty.read(object);
            QtProperty      *property = NULL;

            qDebug() << "property : " << metaProperty.name() << " : " << value.toInt();

            if (metaProperty.type() == QVariant::Int)
            {
                property = _intManager->addProperty(metaProperty.name());
                _intManager->setValue(property, value.toInt());
            }
            else if (metaProperty.type() == QVariant::Double)
            {
                property = _doubleManager->addProperty(metaProperty.name());
                _doubleManager->setValue(property, value.toDouble());
            }
            else if (metaProperty.type() == QVariant::String)
            {
                property = _stringManager->addProperty(metaProperty.name());
                _stringManager->setValue(property, value.toString());
            }

            if (property)
                _browser->addProperty(property);
        }
    }
}
+9

samples QtPropertyBrowser (object_controller), , , , . QWidget ObjectController, setObject (QObject * object); a QObject. , , , QObject, , , . Qt.

0

Source: https://habr.com/ru/post/1775910/


All Articles