[英]C++ QT5 dynamic property
I am using a widget with Q_PROPERTY
entries.我正在使用带有Q_PROPERTY
条目的小部件。 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"
).现在,我确实有一个内部映射,并且我想为该列表中的每个条目添加一个动态属性(名称,例如"entry1Color"
)。
I can add a dynamic property via setProperty("entry1Color", Qt::green);
我可以通过setProperty("entry1Color", Qt::green);
添加一个动态属性setProperty("entry1Color", Qt::green);
successfully, but I don't have a clue where the value ( Qt::green
) is being transferred to.成功,但我不知道值( Qt::green
)被转移到哪里。 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.当您使用setProperty
,该值直接存储在您的 QObject 中,您可以使用property
getter来检索它。 The value is returned as a QVariant
, so you will have to cast it to the appropriate type.该值作为QVariant
返回,因此您必须将其转换为适当的类型。 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.使用Q_PROPERTY
显式声明的property
可以通过property
getter 以完全相同的方式访问。 This is the same mechanism that is used by the QML engine to access your object properties, with setProperty
and property
.这与 QML 引擎用于访问对象属性的机制相同,使用setProperty
和property
。
Qt consistently use setValue()
for setters, and value()
(note the absence of get ) for getters. Qt 始终将setValue()
用于 setter,并将value()
(注意没有get )用于 getter。 Which is probably why you missed the getter in the first place :).这可能就是您首先错过 getter 的原因:)。
When you are using QObject::setProperty on instance of QObject, it will be saved internally in QObject instance.当您在 QObject 实例上使用 QObject::setProperty 时,它会在内部保存在 QObject 实例中。
As I understand you want to implement it as QMap with value as member variable.据我了解,您希望将其实现为 QMap,并将值作为成员变量。 Here how it can be implemented:这里是如何实现的:
testclass.h测试类.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测试类.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.而且,检查 QMap 的工作方式以及它对对的限制也很有用。
When you are using QObject::setProperty on instance of QObject, it will be saved internally in QObject instance.当您在 QObject 实例上使用 QObject::setProperty 时,它会在内部保存在 QObject 实例中。
@Dmitriy: Thanks for clarification and the example code. @Dmitriy:感谢您的澄清和示例代码。 Now I do can read the value set by setProperty, fine so far.现在我可以读取 setProperty 设置的值,到目前为止还好。
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.我想要某种由动态属性设置器调用的 set 函数,例如静态 Q_PROPERTY 条目的 WRITE fn 声明。
In my case, I create a dynamic property
by calling setProperty("entry1Color") just in time with mColors.insert
call.就我而言,我通过调用 setProperty("entry1Color") 与mColors.insert
调用及时创建了一个dynamic property
。 The value should be written directly in my map["entry1Color"].该值应直接写入我的地图 ["entry1Color"]。 I did not yet stumble upon any idea to achieve this.我还没有偶然发现任何想法来实现这一目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.