gpt4 book ai didi

qt - 是否可以在不使用成员函数的情况下实现 Q_PROPERTY READ/WRITE 访问器?

转载 作者:行者123 更新时间:2023-12-04 14:42:55 25 4
gpt4 key购买 nike

通常,很多代码除了获取/设置类成员之外什么都不做。为此,我实现了一个简单的容器类来关联 getter 和 setter
到一个“领域”。乍一看,这看起来还不错,而且代码少得多。这是容器类的样子:

成员(member).h

#include <functional>

template <class T>
class Member
{
public:
T data;

using Getter_t = std::function<T(void)>;
using Setter_t = std::function<void(T)>;
using Notify_t = std::function<void(void)>;

Setter_t m_setterFunc;
Getter_t m_getterFunc;
Notify_t m_notifyFunc;

Member()
{
this->m_getterFunc = [=] (void) -> T { return this->data; };
this->m_setterFunc = [=] (T data) -> void { this->data = data; };
this->m_notifyFunc = [] (void) -> void { };
}

auto get() -> T { return this->m_getterFunc(); }
auto set(T data) -> void { this->m_setterFunc(data); this->m_notifyFunc(); }

auto getter(Getter_t func) -> Member& { this->m_getterFunc = func; return *this; }
auto setter(Setter_t func) -> Member& { this->m_setterFunc = func; return *this; }
auto notify(Notify_t func) -> Member& { this->m_notifyFunc = func; return *this; }

~Member() { }
};

我知道有些事情还不完美,但现在没关系。接下来的几行显示了 Member定义实例和访问底层数据的简单方便的方法。 get , setnotify函数可以被 lambdas 或函数指针替换以覆盖自定义行为。

主.cpp
#include <iostream>

#include "Member.h"

class MyClass
{
public:
Member<int> foo;
Member<std::string> bar;

void barChanged() { std::cout << "bar changed\n"; }
};

auto main(int argc, const char * argv[]) -> int
{
MyClass instance;

instance.foo.notify([] () -> void { std::cout << "foo changed\n"; });
instance.bar.notify(std::bind(&MyClass::barChanged, instance));

instance.foo.set(10);
instance.bar.set("some string");

std::cout << instance.foo.get() << " " << instance.bar.get() << std::endl;

return 0;
}

现在的问题是 Q_PROPERTY宏需要 READ 的函数名称和 WRITE访问器,我又回到了开始的地方:我必须为每个属性显式地编写 get 和 set 函数。正是我想要避免的。
class MyOtherClass : public QObject
{
Q_OBJECT
Q_PROPERTY(bool flag READ getFlag WRITE setFlag NOTIFY flagChanged);
public:
Member<bool> m_flag;
auto getFlag() -> bool { return m_flag.get(); }
auto setFlag(bool flag) -> void { this->m_flag.set(flag); }
};

是否可以直接使用已有的 m_flag.getm_flag.set职能?我尝试了显而易见的事情,但它们要么被 moc 拒绝,要么导致代码过多。

编辑

如下所述, MEMBER关键字可以在不指定 get 和 set 函数的情况下拥有属性。然而,私有(private)成员只能通过他们的名字( this->property("myPropertyName") )访问,而且除了“普通”获取和设置之外,没有其他方法可以实现。

说得更清楚一点:动机不仅仅是避免编写 get 和 set 函数,而是尝试实现一个灵活的成员系统,它
  • 默认情况下按预期执行 get/set
  • 支持自定义逻辑(例如将新设置的值转发给其他实例)
  • 可用于 C++ 类成员并与 Qt 属性兼容

  • 唯一缺少的部分是 Q_PROPERTY 之间的桥梁。 READ/ WRITE Member 的访问器和 get/set 方法类(class)。

    谢谢你的帮助!

    最佳答案

    我认为不可能重定向 READWRITE其他内部或外部对象的属性方法,无需编写包装器,但如果您的 getter 和 setter 除了返回或设置数据之外不做任何事情:有 MEMBER至少在最新的 Qt 版本中变量关联。

    来自 Qt Doc :

    Q_PROPERTY(type name
    (READ getFunction [WRITE setFunction] |
    MEMBER memberName [(READ getFunction | WRITE setFunction)])
    [RESET resetFunction]
    [NOTIFY notifySignal]
    [REVISION int]
    [DESIGNABLE bool]
    [SCRIPTABLE bool]
    [STORED bool]
    [USER bool]
    [CONSTANT]
    [FINAL])

    A READ accessor function is required if no MEMBER variable was specified. It is for reading the property value. Ideally, a const function is used for this purpose, and it must return either the property's type or a const reference to that type. e.g., QWidget::focus is a read-only property with READ function, QWidget::hasFocus().

    A WRITE accessor function is optional. It is for setting the property value. It must return void and must take exactly one argument, either of the property's type or a pointer or reference to that type. e.g., QWidget::enabled has the WRITE function QWidget::setEnabled(). Read-only properties do not need WRITE functions. e.g., QWidget::focus has no WRITE function.

    A MEMBER variable association is required if no READ accessor function is specified. This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. It's still possible to use READ or WRITE accessor functions in addition to MEMBER variable association (but not both), if you need to control the variable access.



    使用 MEMBER您不需要编写 getter 和 setter。

    关于qt - 是否可以在不使用成员函数的情况下实现 Q_PROPERTY READ/WRITE 访问器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168832/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com