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

117 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。 头图

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)
 {
 ​
 }
 ​

在主插件代码中,我也声明了自定义的类,也把自定义类作为属性写上去了,但是还是不可以, 希望有大佬可以指点一下啊。

image-20211111081359330

奈何,在QtDesigner中就是看不到。

image-20211111081659039

放到Qt Creator总也是识别不出来,哪里还有问题呢,大神呢?

image-20211111082156262

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

再提升一下,给用户一个选择呢,咋搞呢,如下图的样子。看我后面的文章

image-20211107132204167

☞ 源码

源码链接:GitHub仓库自取

使用方法:☟☟☟