Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QAbstractListModel, ListView and crashes
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel, ListView and crashes

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 999 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marmistrz
    wrote on last edited by
    #1

    Hi,

    I use QAbstractListModel with a SectionScroller and ListView. My QAbstractListModel functions look like this (I post the item class too)

    @class IrregularListWrapper : public QAbstractListModel
    {
    Q_OBJECT
    Q_PROPERTY(QString langName READ getLangName NOTIFY langChanged)
    Q_PROPERTY(int count READ rowCount NOTIFY langChanged)
    Q_ENUMS(Language)
    public:

    Q_INVOKABLE int rowCount(const QModelIndex& = QModelIndex()) const { return db->count(); }
    Q_INVOKABLE QObject* get(int index) const {return db->at(index);}
    QVariant data(const QModelIndex &index, int role) const;
    
    enum Language
    {
        English = 0,
        German = 1
    };
    
    enum IrregularVerbRoles
    {
        Form0Role = Qt::UserRole + 1,
        Form1Role,
        Form2Role
    };
    
    IrregularListWrapper();
    
    QString getLangName() const { return langName; }
    Q_INVOKABLE void changeLang(Language l) { beginResetModel(); db = 0; setLang(l); endResetModel(); }
    
    static QMap<Language, QString> plugins;
    

    signals:
    void langChanged();
    protected:
    void setLang(Language);
    QString langName;
    AbstractIrregularList * db;

    };

    QMap<IrregularListWrapper::Language, QString> IrregularListWrapper::plugins;

    IrregularListWrapper::IrregularListWrapper()
    {
    QHash<int, QByteArray> roles;
    roles[Form0Role] = "form0";
    roles[Form1Role] = "form1";
    roles[Form2Role] = "form2";

    // populating plugins with plugin locations 
    
    setRoleNames(roles);
    setLang(German);
    

    }

    QVariant IrregularListWrapper::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid()) return QVariant();
    const IrregularVerb* verb = db->at(index.row());

    switch (role)
    {
    case Form0Role:
        return verb->getForm0();
        break;
    case Form1Role:
        return verb->getForm1();
        break;
    case Form2Role:
        return verb->getForm2();
        break;
    }
    return QVariant();
    

    }

    void IrregularListWrapper::setLang(Language l)
    {
    QPluginLoader loader(plugins[l]);
    db = qobject_cast<AbstractIrregularList*>(loader.instance());

    if (db == 0) db = new AbstractIrregularList;
    
    switch (l)
    {
    case English:
        langName = "English";
        break;
    case German:
        langName = "German";
        break;
    }
    emit langChanged();
    

    }
    @

    IrregularVerb.h:

    @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 forms[0]; }
    QString getForm1() const { return forms[1]; }
    QString getForm2() const { return forms[2]; }
    IrregularVerb(QString a, QString b, QString c) { forms[0] = a; forms[1] = b; forms[2] = c; }
    signals:
    void formChanged();
    };
    @

    When scrolling (even normally, without SectionScroller/FastScroll), I'm getting a Segmentation fault. The backtrace is (omitted some addresses)

    @#0 QBasicAtomicInt::ref (this=0x18)
    #1 QString (this=0xbe88d2a0, other=...)
    #2 IrregularVerb::getForm2 (this=0x9e6de8)
    #3 IrregularVerbWrapper::data(this=0x9e31b8, index=..., role=35) // the model
    // some calls to libQtDeclarative@

    Do you have an idea why it's happening? Thanks!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved