1

我无法将标志用作 QT 自定义小部件属性类型。预期的属性未显示在 QT Designer 属性编辑器上。

代码如下:

#ifndef SVBASICDEMO_H
#define SVBASICDEMO_H

#include <QString>
#include <QtUiPlugin/QDesignerExportWidget>
#include <QWidget>
#include <QLabel>
#include "svbasicwidget.h"

QT_BEGIN_NAMESPACE

namespace Company {
namespace Product {
namespace Widget {
namespace Basic {
class QDESIGNER_WIDGET_EXPORT SvBasicDemo : public SvBasicWidget
{
    Q_OBJECT
        Q_FLAGS(SvBasicDemo::AccessTypes)
        Q_PROPERTY(QString url_prop MEMBER url_prop NOTIFY propertyChange)
        Q_PROPERTY(Priority enum_prop MEMBER enum_prop NOTIFY propertyChange)
        Q_PROPERTY(AccessTypes flag_prop MEMBER flag_prop NOTIFY propertyChange)
        Q_PROPERTY(Qt::Alignment flag_prop_2 MEMBER flag_prop_2 NOTIFY propertyChange)

    Q_SIGNALS:
    void propertyChange();

    public slots:
    //更新属性的槽
    void updateProperty();

public:
    SvBasicDemo(QWidget *parent = 0);
    virtual void refresh_binding_cb();
    virtual QString help_info() const;

    enum Priority { High, Low, VeryHigh, VeryLow };
    Q_ENUM(Priority)

    enum AccessType { Read = 0x1, Write = 0x2 };

    Q_DECLARE_FLAGS(AccessTypes, AccessType)

private:
    QString url_prop;
    Priority enum_prop;
    AccessTypes flag_prop;
    Qt::Alignment flag_prop_2;
    QLabel label;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(SvBasicDemo::AccessTypes)

}
}
}
}

QT_END_NAMESPACE
#endif

qt Designer 属性编辑器中的结果如下图所示: 在此处输入图像描述

您可以看到属性 enum_prop(使用自定义枚举)和 flag_prop_2(使用内置标志 Qt::Alignment)按预期工作。但是缺少属性 flag_prop。

我研究了“%QT_ROOT%\Qt5.6.0\5.6\msvc2013\include\QtWidgets\qgraphicsview.h”中的代码。并没有弄清楚为什么。

谁能给我一些提示,说明为什么它不能按预期工作?提前致谢。

4

2 回答 2

1

经过多次尝试,终于解决了这个问题。关键点是:

  1. 看来 MEMBER 关键字不适用于 Q_FLAGS 属性;

  2. Q_DECLARE_OPERATORS_FOR_FLAGS 宏必须在全局命名空间中使用;

    namespace Company {
    
        namespace Product {
        namespace Widget {
        namespace Basic {
        class QDESIGNER_WIDGET_EXPORT SvBasicDemo : public SvBasicWidget
        {
            Q_OBJECT
                Q_FLAGS(AccessTypes)
                Q_PROPERTY(QString url_prop MEMBER url_prop NOTIFY propertyChange)
                Q_PROPERTY(Priority enum_prop MEMBER enum_prop NOTIFY propertyChange)
                // Q_PROPERTY(AccessTypes flag_prop MEMBER flag_prop NOTIFY propertyChange) // Cannot use MEMBER here, otherwise, the property will not show on qt designer property editor. I don't know why.
                Q_PROPERTY(AccessTypes flag_prop READ getAccessTypes WRITE setAccessTypes)
                Q_PROPERTY(Qt::Alignment flag_prop_3 MEMBER flag_prop_2 NOTIFY propertyChange)
    
        Q_SIGNALS:
        void propertyChange();
    
        public slots:
        void updateProperty();
    
    public:
        SvBasicDemo(QWidget *parent = 0);
        virtual void refresh_binding_cb();
        virtual QString help_info() const;
    
        enum Priority { High, Low, VeryHigh, VeryLow };
        Q_ENUM(Priority)
    
        enum AccessType { None = 0x0, Read = 0x1, Write = 0x2 };
        Q_DECLARE_FLAGS(AccessTypes, AccessType)
    
        inline AccessTypes getAccessTypes() const
        {
            return flag_prop;
        }
        inline void setAccessTypes(AccessTypes features)
        {
            flag_prop = features;
        }
    
    private:
        QString url_prop;
        Priority enum_prop;
        AccessTypes flag_prop;
        Qt::Alignment flag_prop_2;
        QLabel label;
    };
    }
    }
    }
    }
    // must use this macro outside namespace
    Q_DECLARE_OPERATORS_FOR_FLAGS(Company::Product::Widget::Basic::SvBasicDemo::AccessTypes)
    
于 2017-02-21T10:13:37.433 回答
0

您是否添加了Q_DECLARE_FLAGS宏?另请参阅Q_FLAGS()的正确用法

这是 WindowsStateFlags 定义的示例:

class MyClass
{
public:
    enum Option {
        NoOptions = 0x0,
        ShowTabs = 0x1,
        ShowAll = 0x2,
        SqueezeBlank = 0x4
    };
    Q_DECLARE_FLAGS(Options, Option)
    Q_FLAGS(Options)
    ...
};

Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Option)

您必须添加属性宏和可调用函数才能获得当前选项:

Q_PROPERTY(Option option READ getOption())
Q_INVOKABLE Option getOption() const;

并注册元类型qRegisterMetaType或 Q_DECLARE_METATYPE():

Q_DECLARE_METATYPE(MyClass::Option);
于 2017-02-20T15:48:18.233 回答