0
votes

I use QList<QObject*> as a model in my app. As there might be a lot of elements, I decided to use SectionScroller. When I try to scroll using the SectionScroller I'm getting a

Error: Unable to assign [undefined] to QString

What am I doing wrong?

My ListView is:

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

Thanks

EDIT: more code:

the irregulars header

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

and the body of a function

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

the classes german, english are like that

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

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

and 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();

};

Edit 2: this doesn't work If I do

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

If I remove the star, the error is similar.

EDIT3:

I found out this http://ruedigergad.com/2011/08/22/qml-sectionscroller-vs-qabstractlistmodel/

And found out that irregulars.db.get is undefined

And changed german and english to

class german : public AbstractIrregularList

and

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

But even now, irregulars.db.get(0) gives error (Result of expression 'irregulars.db.get' [undefined] is not a function.)

Why is happening like that, that the Q_INVOKABLE is not detected? The Q_OBJECT macro is there

/edit5: Even when using QVariant the errors are still there. It can be either treated as QList or as QObject*.

1

1 Answers

1
votes

If I am not wrong, you should use QVariantList instead QList<SomeClass> to expose list of elements from C++ to QML.

It should solve problem.


Supported types in QML


Try making code to look like this (in irregulars header):

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

Or if you want to code look like from links you gave:

Q_PROPERTY(QVariantList db READ getdb NOTIFY langChanged)
//...
QVariant getdb() const {return QVariant::fromValue(db);}