QML SectionScroller和QList <qobject *> </qobject *>

时间:2013-01-31 13:16:52

标签: qt listview scroll qml qlist

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

Error: Unable to assign [undefined] to QString

我做错了什么?

我的ListView是:

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*/
}

和IrregularVerb:

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>}'

如果删除星号,则错误类似。

EDIT3:

我发现了这个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);}
};

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

为什么会这样发生,没有检测到Q_INVOKABLE? Q_OBJECT宏在那里

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

1 个答案:

答案 0 :(得分:1)

如果我没有错,你应该使用QVariantList而不是QList&lt; SomeClass&gt;公开从C ++到QML的元素列表。

它应该解决问题。


Supported types in QML


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

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);}