0

QList<QObject*>在我的应用程序中用作模型。由于可能有很多元素,我决定使用 SectionScroller。当我尝试使用 SectionScroller 滚动时,我得到一个

Error: Unable to assign [undefined] to QString

我究竟做错了什么?

我的列表视图是:

ListView
{
    id: irrview
    width: parent.width
    model: irregulars.db // QList<QObject*>
    anchors.top: caption.bottom
    anchors.bottom: parent.bottom
    clip: true
    section.criteria: ViewSection.FirstCharacter
    section.property: "form0"
    section.delegate: Item {height: 10; width: parent.width; Text { text: section } } // for testing purposes
    delegate: Rectangle
    {
        /**/
    }
}

谢谢

编辑:更多代码:

irregulars标题_

class IrregularListWrapper : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<QObject*> db READ getdb NOTIFY langChanged)
    Q_ENUMS(Language)
public:
    enum Language
    {
        English = 0,
        German = 1
    };

    IrregularListWrapper() : db(0) { setLang(German); }
    ~IrregularListWrapper() { delete db; }
    QList<QObject*> getdb() const { return *db; }

    Q_INVOKABLE void changeLang(Language l) { delete db; setLang(l); }


signals:
    void langChanged();
protected:
    void setLang(Language);
    QList<QObject*> * db;
};

和函数体

void IrregularListWrapper::setLang(Language l)
{
    switch (l)
    {
    case English:
        db = new english;
        langName = "English";
        break;
    case German:
        db = new german;
        langName = "German";
        break;
    }
    emit langChanged();
}

德语、英语的课是这样的

class german : public QList<QObject*>
{
public:
    german();
};

german::german()
{
    append(new IrregularVerb("anfangen", "fing an", "angefangen"));
    /*more like that*/
}

和不规则动词:

class IrregularVerb : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString form0 READ getForm0 NOTIFY formChanged)
    Q_PROPERTY(QString form1 READ getForm1 NOTIFY formChanged)
    Q_PROPERTY(QString form2 READ getForm2 NOTIFY formChanged)
public:
    QString forms[3];
    QString getForm0() const { return getForm(0); }
    QString getForm1() const { return getForm(1); }
    QString getForm2() const { return getForm(2); }
    IrregularVerb(QString a, QString b, QString c) { forms[0] = a; forms[1] = b; forms[2] = c; }
protected:
    const QString& getForm(const int& ind) const { return forms[ind]; }
signals:
    void formChanged();

};

编辑2:这不起作用如果我这样做

QVariantList getdb() const { return QVariant::fromValue(*db); }

IrregularListWrapper.h:24: error: could not convert 'QVariant::fromValue(const T&) [with T = QList<QObject*>; QVariant = QVariant]()' from 'QVariant' to 'QVariantList {aka QList<QVariant>}'

如果我去掉星号,错误是相似的。

编辑3:

我发现了这个http://ruedigergad.com/2011/08/22/qml-sectionscroller-vs-qabstractlistmodel/

并发现这irregulars.db.get是未定义的

并将德语和英语改为

class german : public AbstractIrregularList

class AbstractIrregularList : public QObject, public QList<QObject*>
{
    Q_OBJECT
public:
    Q_INVOKABLE QObject* get(int index) {return at(index);}
};

但即使是现在,regulars.db.get(0) 也会出错(表达式 'irregulars.db.get' [undefined] 的结果不是函数。)

为什么会发生这样的情况,即未检测到 Q_INVOKABLE?Q_OBJECT 宏在那里

/edit5:即使使用 QVariant,错误仍然存​​在。它可以被视为 QList 或 QObject*。

4

1 回答 1

1

如果我没记错的话,您应该使用 QVariantList 而不是 QList<SomeClass> 将元素列表从 C++ 公开到 QML。

它应该解决问题。


QML 中支持的类型


尝试使代码看起来像这样(在不规则标题中):

Q_PROPERTY(QVariantList db READ getdb NOTIFY langChanged)
//...
QVariantList getdb() const {/*convert db to QVariantList*/ return converted_db;}

或者,如果您想从您提供的链接中编写代码:

Q_PROPERTY(QVariantList db READ getdb NOTIFY langChanged)
//...
QVariant getdb() const {return QVariant::fromValue(db);}
于 2013-01-31T13:44:46.240 回答