C++ 设置QList子对象

C++ 设置QList子对象,c++,qt,qml,C++,Qt,Qml,你好 我正在设置一个对象以在QML中获取它。。。在这个对象定义中,我得到 (location_in_my_computer):25: candidate constructor not viable: no known conversion from 'QList<QString>' to 'QList<const QString> &' for 10th argument GroupObject(..., ^ (我的计算机中的位置):25:候选构造

你好

我正在设置一个对象以在QML中获取它。。。在这个对象定义中,我得到

(location_in_my_computer):25: candidate constructor 
not viable: no known conversion from 'QList<QString>' to 'QList<const QString> &' 
for 10th argument GroupObject(...,
    ^
(我的计算机中的位置):25:候选构造函数
不可行:没有从'QList'到'QList&'的已知转换
对于第10个参数GroupObject(。。。,
^
在我的代码中,我使用以下类(最少的示例):

class GroupObject:公共QObject
{ 
公众:
GroupObject(QObject*parent=0);
GroupObject(
QList&tags,QObject*parent=0);
QList标签();
无效设置标签(QList和标签);
私人:
QList m_标签;
}; 
以及他的实施:

#include "groupobject.h"

GroupObject::GroupObject( QList<const QString> &tags, QObject *parent) QObject(parent),
    m_tags(tags){

    }

QList<const QString> GroupObject::tags()
{
    return m_tags;
}

void GroupObject::setTags(QList<const QString> &tags)
{
    if(tags != m_tags){
        m_tags = tags;
    }
}
#包括“groupobject.h”
GroupObject::GroupObject(QList和标记,QObject*父对象)QObject(父对象),
m_标签(标签){
}
QList GroupObject::tags()
{
返回m_标签;
}
void GroupObject::setTags(QList和tags)
{
如果(标记!=m_标记){
m_标签=标签;
}
}
在下面的示例中,我调用设置GroupObject的一个QList:

QList<QString> tags;
QList<QObject*> dataList;
dataList.append( new GroupObject( tags ));
QList标签;
QList数据表;
追加(新的GroupObject(标记));
我如何在正确的概念下做到这一点


谢谢标签的类型是
QList
,但是
GroupObject
构造函数接受
QList

实际上,用于
QString
const
修饰符在
QList
中没有任何意义,因为它不保护
QList
修改。它只是拒绝修改
QList
项。在这种情况下,在
QList
初始化期间,您甚至不能复制一个这样的
QList

因此,要编译代码,您必须通过
QList
更改
QList
。在某些地方,您可能还希望防止修改实际的
QList
对象,例如:

// do not allow GroupObject(...) to change external 'tags' instance
GroupObject(const QList<QString> &tags, QObject *parent=0);

// return reference to internal object field and
// do not allow callers to use that reference for changing that internal field
// it does not change instance of GroupObject, so
// there is 'const' modifier of the member function.
const QList<QString>& tags() const;

// or provide full copy of returned object
QList<QString> tags() const;

// do not allow to change external 'tags' inside 'setTags()'
void setTags(const QList<QString> &tags);
//不允许GroupObject(…)更改外部“标记”实例
GroupObject(常量QList和标记,QObject*parent=0);
//返回对内部对象字段和
//不允许调用方使用该引用更改该内部字段
//它不会更改GroupObject的实例,因此
//成员函数中有“const”修饰符。
常量QList&tags()常量;
//或者提供返回对象的完整副本
QList tags()常量;
//不允许更改“setTags()”内部的外部“标记”
无效设置标签(常量QList和标签);

顺便说一句,Qt中有
QStringList
类,用于提供附加功能的
QString
列表。

标签的类型是
QList
,但是
GroupObject
构造函数接受
QList

实际上,用于
QString
const
修饰符在
QList
中没有任何意义,因为它不保护
QList
修改。它只是拒绝修改
QList
项。在这种情况下,在
QList
初始化期间,您甚至不能复制一个这样的
QList

因此,要编译代码,您必须通过
QList
更改
QList
。在某些地方,您可能还希望防止修改实际的
QList
对象,例如:

// do not allow GroupObject(...) to change external 'tags' instance
GroupObject(const QList<QString> &tags, QObject *parent=0);

// return reference to internal object field and
// do not allow callers to use that reference for changing that internal field
// it does not change instance of GroupObject, so
// there is 'const' modifier of the member function.
const QList<QString>& tags() const;

// or provide full copy of returned object
QList<QString> tags() const;

// do not allow to change external 'tags' inside 'setTags()'
void setTags(const QList<QString> &tags);
//不允许GroupObject(…)更改外部“标记”实例
GroupObject(常量QList和标记,QObject*parent=0);
//返回对内部对象字段和
//不允许调用方使用该引用更改该内部字段
//它不会更改GroupObject的实例,因此
//成员函数中有“const”修饰符。
常量QList&tags()常量;
//或者提供返回对象的完整副本
QList tags()常量;
//不允许更改“setTags()”内部的外部“标记”
无效设置标签(常量QList和标签);

顺便说一句,Qt中有
QStringList
类,用于提供附加功能的
QString
列表。

如果不需要按值传递,那么
QList dataList
是不必要的。
QObject
也是对象的所有者容器。因此,您还可以编写:

class F {
  QObject data;
  ...
  void foo() {
    QStringList tags = ...;
    new GroupObject(tags, &data);
    ...
  }
  void bar() {
    // iterate all data objects
    for (auto obj : data.children()) {
      auto group = qobject_cast<GroupObject>(obj);
      if (group) { qDebug() << group.tags(); continue; }
      ...
    }
  }
}
F类{
QObject数据;
...
void foo(){
QStringList标签=。。。;
新的GroupObject(标记和数据);
...
}
空条(){
//迭代所有数据对象
对于(自动对象:data.children()){
自动组=qobject_cast(obj);

如果(group){qDebug()您不需要通过值传递它,那么
QList dataList
是不必要的。
QObject
也是对象的所有者容器。因此您还可以编写:

class F {
  QObject data;
  ...
  void foo() {
    QStringList tags = ...;
    new GroupObject(tags, &data);
    ...
  }
  void bar() {
    // iterate all data objects
    for (auto obj : data.children()) {
      auto group = qobject_cast<GroupObject>(obj);
      if (group) { qDebug() << group.tags(); continue; }
      ...
    }
  }
}
F类{
QObject数据;
...
void foo(){
QStringList标签=。。。;
新的GroupObject(标记和数据);
...
}
空条(){
//迭代所有数据对象
对于(自动对象:data.children()){
自动组=qobject_cast(obj);
if(群){qDebug()