QAbstractListModel, ListView and crashes
-
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!