如何将枚举用作QProperty并使用样式表更新它?

huangapple go评论88阅读模式
英文:

How to use a Enum as QProperty and update it using a stylesheet?

问题

如何将枚举用作 QProperty 并使用样式表更新其值?

class Gallery : public QScrollArea
{
    Q_OBJECT
    Q_PROPERTY(int horizontalSpacing MEMBER horizontalSpacing) 
    Q_PROPERTY(Orientation orientation MEMBER orientation WRITE setOrientation)

public:
    int horizontalSpacing;

    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    void setOrientation(Orientation orientation);
};

使用:

Gallery
{
    qproperty-horizontalSpacing: 30;
    qproperty-orientation: "left";
}

setOrientation 函数不会被调用。
使用:

Gallery
{
    qproperty-horizontalSpacing: 30;
    qproperty-orientation: left;
}

该函数被调用,但是 orientation 的值是 19,这是什么意思?

void Gallery::setOrientation(Orientation orientation)
{
}
英文:

How to use an Enum as QProperty and update its value using a stylesheet?

class Gallery : public QScrollArea
{
    Q_OBJECT
    Q_PROPERTY(int horizontalSpacing MEMBER horizontalSpacing) 
    Q_PROPERTY(Orientation orientation MEMBER orientation WRITE setOrientation)

public:
    int horizontalSpacing;

    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    void setOrientation(Orientation orientation);
};

Using:

Gallery
{
    qproperty-horizontalSpacing: 30;
    qproperty-orientation: "left";
}

the setOrientation function is not called.
Using:

Gallery
{
    qproperty-horizontalSpacing: 30;
    qproperty-orientation: left;
}

The function is called but, the value of orientation is 19, what does this mean?

void Gallery::setOrientation(Orientation orientation)
{
}

答案1

得分: 1

使用一个访问器(在此情况下为WRITE)处理自定义属性时,似乎会导致未定义的行为,这在获取Orientation枚举的int值时会被注意到。

为了解决这个问题,有两种解决方案,都需要添加Q_ENUM(someEnum)


解决方案1:

只使用READWRITE来设置orientation

class Gallery : public QScrollArea
{
    Q_OBJECT
    Q_PROPERTY(Orientation orientation READ getOrientation WRITE setOrientation)

public:
    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    Q_ENUM(Orientation)

    void setOrientation(Orientation orient)
    {
        orientation = orient;
    }
    Orientation getOrientation()
    {
        return orientation;
    }
};

在设置样式表时,引号是必需的,同时要匹配大小写,本例中全部大写:

Gallery
{
    qproperty-orientation: "LEFT";
}

解决方案2:

只使用MEMBER

class Gallery : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(Orientation orientation MEMBER orientation)

public:
    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    Q_ENUM(Orientation)
};

样式表的规则相同。


我将包括来自这个答案的解释,关于Q_PROPERTY: MEMBER vs READ/WRITE

…您可以使用MEMBER并依赖于自动生成的平凡访问器函数,或者如果它们比默认函数复杂,可以自己定义这些函数。


了解更多:

英文:

It seems using MEMBER with one accessor (WRITE in this case) to handle a custom property, causes undefined behavior that is noticed when fetching the int value of Orientation enum.

To work around that, here are 2 solutions, and both require adding Q_ENUM(someEnum):


Solution 1:

Use only READ and WRITE to set orientation:

class Gallery : public QScrollArea
{
    Q_OBJECT
    Q_PROPERTY(Orientation orientation READ getOrientation WRITE setOrientation)

public:
    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    Q_ENUM(Orientation)

    void setOrientation(Orientation orient)
    {
        orientation = orient;
    }
    Orientation getOrientation()
    {
        return orientation;
    }
};

When setting stylesheet, quotes are mandatory, as well as matching the casing, which is all uppercase in this case:

Gallery
{
    qproperty-orientation: "LEFT";
}

Solution 2:

Use MEMBER on its own:

class Gallery : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(Orientation orientation MEMBER orientation)

public:
    enum Orientation { TOP, BOTTOM, LEFT, RIGHT };
    Orientation orientation;
    Q_ENUM(Orientation)
};

Same rules for the stylesheet.


I'll include this explanation from this answer on Q_PROPERTY: MEMBER vs READ/WRITE:

>...you can either use MEMBER and rely on auto generated, trivial accessor functions or define for yourself those functions if they happen to be more complex than a defaulted one.


For more:

huangapple
  • 本文由 发表于 2023年7月11日 05:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定