C++,将 QList<T> 分配给 QList<T> 时出现 QT 错误

C++,QT Error When Assigning QList<T> to QList<T>

我正在使用 C++ 开发 QML 应用程序,但我目前遇到一个可能很简单的错误:

C:\Qt.2.1\mingw48_32\include\QtCore\qvector.h:679: error: no match for 'operator==' (operand types are 'ListModel' and 'ListModel') if (!(*--i == *--j)) ^

我的 header 是:

#ifndef COMBOBOXUPDATE_H
#define COMBOBOXUPDATE_H
#include <QObject>
#include <QStringList>
#include <QString>
#include <QVector>

struct ListModel;

class ComboboxUpdate:public  QObject
{
Q_OBJECT
Q_PROPERTY(QVector<ListModel> comboList READ comboList)

public:

  ComboboxUpdate(QObject *parent = 0);
  QVector<ListModel> comboList();
  void setComboList( QVector<ListModel> &comboList);

private:
QVector<ListModel> m_comboList;
int         m_count;
};

struct  ListModel
{
ListModel();
ListModel(QString _text,int _Sqlid)
{
    text=_text;
    Sqlid=_Sqlid;
}
QString text;
int     Sqlid;
};
#endif // COMBOBOXUPDATE_H

错误发生在实现文件中的代码区:

void ComboboxUpdate::setComboList(  QVector<ListModel> &comboList)
{
    if (m_comboList != comboList)
    {
        m_comboList = comboList;
    }
}

我不明白为什么会出现这个问题。我的主要目标是使用 ListElement 之类的东西从 C++ 端填充组合框。我可以使用 QStringList 成功填充。但是我想像 ListElement 一样填写。例如:

ComboBox {
    model: ListModel {
               ListElement {sqlid:"1"; text:"Pansi"}
               ListElement {sqlid:"2"; text:"Rose"}
               ListElement {sqlid:"3"; text:"Clips"}
           }
    anchors.fill: parent
}

在 QML 端,此模型在 ComboBox 中显示文本并将值存储到 sqlite 中。我怎样才能在 C++ 方面做到这一点?

您需要为您的 class ListModel 提供 operator==。例如:

struct  ListModel
{
    bool operator==(const ListModel& other) const {
        return other.text == text && other.Sqlid == Sqlid;
    }
};