QtApplets-自定义控件-4-属性研究

QtApplets-自定义控件-4-属性研究

QtApplets-自定义控件-4-属性研究

这一篇我们研究研究自定控件中属性部分。也终于要添加我们自己的代码了。先看下演示效果吧。这里我搞了一个名字叫做testID的属性,他对应的读函数为getTestID写函数为setTestID

QtApplets-自定义控件-4-属性研究QtApplets-自定义控件-4-属性研究

QtApplets-自定义控件-4-属性研究

1 声明一个自定义的属性

2 实现

☞ 源码

关键字: Q_PROPERTY、属性、自定义、设置、获取


1 声明一个自定义的属性

这里我们需要用到一个关键宏Q_PROPERTY,看看官方对这个宏的描述


This macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System.


简单翻译一下:


这个宏用于在继承QObject的类中声明属性,属性的行为类似于数据成员,但是它们有通过元对象系统访问的附加特性。

  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])

QtApplets-自定义控件-4-属性研究

#include "customcontrol.h"

CustomControl::CustomControl(QWidget *parent) :
    QWidget(parent)
{
    m_label = new QLabel(this);
    m_label->setGeometry(0,0,100,20);
}

int CustomControl::getTestID()
{
    return testID;
}

void CustomControl::setTestID(int temp)
{
    testID = temp;
    m_label->setGeometry(0,0,100,20);
    m_label->setStyleSheet("color: rgb(255, 255, 0);font: 11pt '黑体';");
    m_label->setText(QString::number(testID,10));
    update();
}

QtApplets-自定义控件-4-属性研究

上一篇:QtApplets-国际化多语言设置


下一篇:Microsoft Visual Studio与Firefly 一直提示加载项目,更新源码状态问题