【问题标题】:C++,QT Error When Assigning QList<T> to QList<T>将 QList<T> 分配给 QList<T> 时出现 C++、QT 错误
【发布时间】:2015-12-12 07:32:15
【问题描述】:

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

C:\Qt\5.2.1\mingw48_32\include\QtCore\qvector.h:679: 错误:'operator==' 不匹配(操作数类型为 'ListModel' 和 'ListModel') 如果 (!(*--i == *--j)) ^

我的标题是:

#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++ 端做到这一点?

【问题讨论】:

    标签: qt struct qml qt5 qtquick2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多