简体   繁体   中英

C++ QT5 dynamic property

I am using a widget with Q_PROPERTY entries. Now, I do have an internal map, and for each entry in that list has I'd like to add a dynamic property (name eg "entry1Color" ).

I can add a dynamic property via setProperty("entry1Color", Qt::green); successfully, but I don't have a clue where the value ( Qt::green ) is being transferred to. How to connect that set value to my map?

When you use setProperty , this value is stored directly in your QObject, and you can use the property getter to retrieve it. The value is returned as a QVariant , so you will have to cast it to the appropriate type. Example for a color:

// The boolean returned indicates if this is a newly created
// or existing  properly
bool was_just_create = myObject->setProperty("myColor", QColor(Qt::green));

// [...]
// And read it later on
QColor color1 = myObject->property("myColor").value<QColor>();

Properties declared explicitly with Q_PROPERTY are accessible in the exact same way, with the property getter. This is the same mechanism that is used by the QML engine to access your object properties, with setProperty and property .

Qt consistently use setValue() for setters, and value() (note the absence of get ) for getters. Which is probably why you missed the getter in the first place :).

When you are using QObject::setProperty on instance of QObject, it will be saved internally in QObject instance.

As I understand you want to implement it as QMap with value as member variable. Here how it can be implemented:

testclass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QMap>
#include <QColor>

class TestClass : public QObject
{
    Q_OBJECT
public:
    explicit TestClass(QObject *parent = 0);

    // mutators
    void setColor(const QString& aName, const QColor& aColor);
    QColor getColor(const QString &aName) const;

private:
    QMap<QString, QColor> mColors;
};

#endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"

TestClass::TestClass(QObject *parent) : QObject(parent)
{

}

void TestClass::setColor(const QString &aName, const QColor &aColor)
{
    mColors.insert(aName, aColor);
}

QColor TestClass::getColor(const QString &aName) const
{
    return mColors.value(aName);
}

main.cpp

#include "mainwindow.h"

#include <QApplication>
#include <QDebug>

#include "testclass.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    TestClass testClass;
    testClass.setColor("entry1Color", Qt::green);

    qDebug() << testClass.getColor("entry1Color");


    return a.exec();
}

But also, it can be useful to check for how QMap works and which limitations for pairs it has.

When you are using QObject::setProperty on instance of QObject, it will be saved internally in QObject instance.

@Dmitriy: Thanks for clarification and the example code. Now I do can read the value set by setProperty, fine so far.

But this is not all I want. I'd like to have some kind of a set function that will be called by the dynamic property setter, like the WRITE fn declaration for static Q_PROPERTY entries.

In my case, I create a dynamic property by calling setProperty("entry1Color") just in time with mColors.insert call. The value should be written directly in my map["entry1Color"]. I did not yet stumble upon any idea to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM