【问题标题】:C++ QT5 dynamic propertyC++ QT5 动态属性
【发布时间】:2017-03-02 09:35:36
【问题描述】:

我正在使用带有 Q_PROPERTY 条目的小部件。现在,我确实有一个内部映射,并且我想为该列表中的每个条目添加一个动态属性(名称,例如 "entry1Color")。

我可以通过setProperty("entry1Color", Qt::green); 成功添加动态属性,但我不知道值(Qt::green)被转移到哪里。 如何将该设置值连接到我的地图?

【问题讨论】:

    标签: c++ qt5 qt5.5


    【解决方案1】:

    当您使用setProperty 时,该值直接存储在您的QObject 中,您可以使用property getter 来检索它。该值作为QVariant 返回,因此您必须将其转换为适当的类型。颜色示例:

    // 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>();
    

    使用Q_PROPERTY 显式声明的属性可以通过property getter 以完全相同的方式访问。这与 QML 引擎用于访问对象属性的机制相同,setPropertyproperty

    Qt 一直使用setValue() 作为setter,value()(注意没有get)作为getter。这可能是你一开始就错过了吸气剂的原因:)。

    【讨论】:

      【解决方案2】:

      当你在 QObject 实例上使用 QObject::setProperty 时,它会被内部保存在 QObject 实例中。

      据我了解,您希望将其实现为 QMap,并将值作为成员变量。 这里是如何实现的:

      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);
      }
      

      ma​​in.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();
      }
      

      此外,检查 QMap 的工作原理以及它对配对有哪些限制也很有用。

      【讨论】:

        【解决方案3】:

        当你在 QObject 实例上使用 QObject::setProperty 时,它会被保存在 QObject 实例内部。

        @Dmitriy:感谢您的澄清和示例代码。 现在我可以读取 setProperty 设置的值了,到目前为止还不错。

        但这不是我想要的。我想要某种由动态属性设置器调用的 set 函数,例如静态 Q_PROPERTY 条目的 WRITE fn 声明。

        在我的例子中,我通过调用 setProperty("entry1Color") 创建了一个 dynamic property mColors.insert 调用。 该值应该直接写在我的地图[“entry1Color”]中。我还没有偶然发现任何实现这一目标的想法。

        【讨论】:

          猜你喜欢
          • 2016-08-25
          • 1970-01-01
          • 1970-01-01
          • 2011-01-29
          • 2011-08-30
          • 1970-01-01
          • 2011-05-05
          • 2012-02-18
          • 2012-10-09
          相关资源
          最近更新 更多