我试图将抽象类的实例用作扩展QQuickItems之一中的属性。我有一个基础课,像这样:
/bin/rrdtool graph - \
--imgformat=PNG \
--start='-86400' \
--end='-300' \
--title='Total Online Users' \
--base='1000' \
--height='150' \
--width='650' \
--alt-autoscale-max \
--lower-limit='0' \
--units-exponent='0' \
--vertical-label='Online Users' \
--slope-mode \
--font TITLE:10: \
--font AXIS:7: \
--font LEGEND:8: \
--font UNIT:7: \
DEF:a='/usr/share/cacti/rra/xxxxxxxxxxxxxxxx.rrd':'dsl_user_input':AVERAGE \
DEF:b='/usr/share/cacti/rra/yyyyyyyyyyyyyyyy.rrd':'time':AVERAGE \
CDEF:cdefa='a,0,*' \
CDEF:cdefb='b,0,*' \
CDEF:cdefc='TIME,1533625072,GT,a,a,UN,0,a,IF,IF,TIME,1533625072,GT,b,b,UN,0,b,IF,IF,+' \
LINE2:cdefa#0000FFFF:'' \
LINE2:cdefb#0000FFFF:'' \
GPRINT:cdefc:LAST:'Total Users\:%8.0lf' \
LINE2:cdefc#0000FFFF:'' \
GPRINT:cdefc:MIN:'Min Users\: %8.0lf' \
GPRINT:cdefc:MAX:'Max Users\: %8.0lf\n'
/bin/rrdtool graph - \
--imgformat=PNG \
--start='-86400' \
--end='-300' \
--title='Total Online Users' \
--base='1000' \
--height='150' \
--width='650' \
--alt-autoscale-max \
--lower-limit='0' \
--units-exponent='0' \
--vertical-label='Online Users' \
--slope-mode \
--font TITLE:10: \
--font AXIS:7: \
--font LEGEND:8: \
--font UNIT:7: \
DEF:a='/usr/share/cacti/rra/xxxxxxxxxxxxxxxx.rrd':'dsl_user_input':AVERAGE \
DEF:b='/usr/share/cacti/rra/yyyyyyyyyyyyyyyy.rrd':'time':AVERAGE \
CDEF:cdefa='a,0,*' \
CDEF:cdefb='b,0,*' \
LINE2:cdefa#0000FFFF:'' \
LINE2:cdefb#0000FFFF:''
是抽象类,它扩展了一些其他类,例如class VizFactory : public QQuickItem
{
Q_OBJECT;
Q_PROPERTY(VisualizationOptions * options READ options WRITE setOptions);
private:
VisualizationOptions * m_visualizationOptions;
public:
VizFactory(QQuickItem * parent = 0) : QQuickItem(parent) {}
~VizFactory() {}
VisualizationOptions * options() const;
void setOptions(VisualizationOptions * options);
};
和VisualizationOptions
:
LayoutOptions
我使用以下命令将所有内容注册到主文件中:
FooOptions
然后,我尝试通过以下方式在qml文件中使用它:
class VisualizationOptions : public QObject
{
Q_OBJECT
public:
VisualizationOptions(QObject * parent = 0) : QObject(parent) {}
virtual std::string getVisualizationName() const = 0;
};
但是,我得到的错误是来自定义qmlRegisterUncreatableType<VisualizationOptions>("Wormhole", 1, 0, "VisualizationOptions",
QString("Cannot create abstract class VisualizationOptions"));
qmlRegisterType<LayoutOptions>("Wormhole", 1, 0, "LayoutOptions");
qmlRegisterType<FooOptions>("Wormhole", 1, 0, "FooOptions");
的行上的qml文件,说:“无法分配给不存在的默认属性”。为什么会这样呢?我没有正确注册类型吗?我在QML中错误地实例化了对象吗?
答案 0 :(得分:2)
问题不是由抽象类引起的,而是因为QObject
不具有childItems
,与具有该属性的QQuickItem
不同。
如果使用QtObject,也会看到相同的内容:
QtObject{
QtObject{
}
}
消息:
无法分配给不存在的默认属性
所以有3种可能的解决方案:
使VisualizationOptions继承自QQuickItem,因此您可以拥有子代。
请勿分配子项,例如,使用您的代码应可实现以下目的:
VizFactory{
options: LayoutOptions{
}
}
我将使用第三种方法显示一个示例:
class VisualizationOptions: public QObject{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<VisualizationOptions> childOptions READ options)
public:
using QObject::QObject;
virtual std::string getVisualizationName() const = 0;
QQmlListProperty<VisualizationOptions> options(){
return QQmlListProperty<VisualizationOptions>(this, this,
&VisualizationOptions::appendOption,
&VisualizationOptions::optionCount,
&VisualizationOptions::option,
&VisualizationOptions::clearOptions);
}
void appendOption(VisualizationOptions* p) {
m_childsoptions.append(p);
}
int optionCount() const{
return m_childsoptions.count();
}
VisualizationOptions *option(int index) const{
return m_childsoptions.at(index);
}
void clearOptions() {
m_childsoptions.clear();
}
private:
static void appendOption(QQmlListProperty<VisualizationOptions>* list, VisualizationOptions* p) {
reinterpret_cast<VisualizationOptions* >(list->data)->appendOption(p);
}
static void clearOptions(QQmlListProperty<VisualizationOptions>* list) {
reinterpret_cast<VisualizationOptions* >(list->data)->clearOptions();
}
static VisualizationOptions* option(QQmlListProperty<VisualizationOptions>* list, int i) {
return reinterpret_cast< VisualizationOptions* >(list->data)->option(i);
}
static int optionCount(QQmlListProperty<VisualizationOptions>* list) {
return reinterpret_cast< VisualizationOptions* >(list->data)->optionCount();
}
QVector<VisualizationOptions *> m_childsoptions;
};
*。qml
VizFactory{
options:
LayoutOptions{
type: "row"
childOptions: [
FooOptions{},
LayoutOptions{
type: "col"
childOptions: [
FooOptions {},
FooOptions {}
]
}
]
}
}
您可以在以下link中找到完整的代码。