QtApplets-自定义控件-6-属性研究(未成功)

QtApplets-自定义控件-6-属性研究(未成功)QtApplets-自定义控件-6-属性研究

接上篇,我们最后的那个升华了的小问题,如何使用自定义的数据类型作为我们自定义控件的属性呢。看帮助文档是支持的,但是要什么样子的自定义数据类型,这里没有写呀。这里我没有搞定,后面的内容不用看了。


The property name and type and the READ function are required. The type can be any type supported by QVariant, or it can be a user-defined type. The other items are optional, but a WRITE function is common. The attributes default to true except USER, which defaults to false.

For example:


Q_PROPERTY(QString title READ title WRITE setTitle USER true)


For more details about how to use this macro, and a more detailed example of its use, see the discussion on Qt’s Property System.

See also Qt’s Property System.


文章目录


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

目前状态

提升一下,给用户来个选择咋样?

☞ 源码

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


目前状态


目前状态呢,我已经实现一个自定义的类,代码如下


testrect.h

#ifndef TESTRECT_H
#define TESTRECT_H

#include <QObject>
#include <QSharedDataPointer>
#include <QWidget>

class TestRectData;

class TestRect : public QWidget
{
    Q_OBJECT

    Q_PROPERTY(int testX READ getTestX WRITE setTestX)

public:
    explicit TestRect(QWidget *parent = nullptr);
    TestRect(const TestRect &);
    TestRect &operator=(const TestRect &);
    ~TestRect();

    int getTestX();

    void setTestX(int temp);




signals:


private:
    QSharedDataPointer<TestRectData> data;

private:
    int mTestX = 0;
};

#endif // TESTRECT_H

testrect.cpp


#include "testrect.h"

class TestRectData : public QSharedData
{
public:

};

TestRect::TestRect(QWidget *parent) : QWidget(parent), data(new TestRectData)
{

}

TestRect::TestRect(const TestRect &rhs) : data(rhs.data)
{

}

TestRect &TestRect::operator=(const TestRect &rhs)
{
    if (this != &rhs)
        data.operator=(rhs.data);
    return *this;
}

TestRect::~TestRect()
{

}

int TestRect::getTestX()
{
    return mTestX;
}

void TestRect::setTestX(int temp)
{

}

QtApplets-自定义控件-6-属性研究(未成功)QtApplets-自定义控件-6-属性研究(未成功)QtApplets-自定义控件-6-属性研究(未成功)QtApplets-自定义控件-6-属性研究(未成功)QtApplets-自定义控件-6-属性研究(未成功)

上一篇:QtApplets-自定义控件-3-插件部署问题


下一篇:冒号初始化与构造函数内赋值