gpt4 book ai didi

c++ - QML ListView 和段错误

转载 作者:太空狗 更新时间:2023-10-29 23:06:26 24 4
gpt4 key购买 nike

我正在使用带有 SectionScrollerQAbstractListModel 的 QML ListView。我注意到当我正常滚动(不使用 SectionScroller)

时,我在 memcpy(从未明确调用)中遇到段错误

你知道为什么会这样吗?

我试过重现,现在段错误是

0x402f9c3a in ?? () from /usr/lib/libQtScript.so.4
0x402f9c3a: ldrh r1, [r7, r3]

调试符号在那里,但没有转储任何有值(value)的信息。另一次段错误是

0x0000cab8 in QBasicAtomicInt::ref (this=0x0) at /usr/include/QtCore/qatomic_armv6.h: 119

这很奇怪,因为 AFAIK N900 的处理器是 armv7/编辑:在 N950 上它使用相同的并且在 Qt 源中仅适用于 ARM qatomic_arm.hqatomic_armv6.h 所以应该没问题。

ListView{
id: irrview
width: parent.width
model: irregulars
anchors.top: caption.bottom
anchors.bottom: parent.bottom
spacing: 5
clip: true
section.criteria: ViewSection.FirstCharacter
section.property: "form0"

delegate: Rectangle{
id: del
property int fontSize: 20
height: 60
width: parent.width
color: "#E0E1E2"
Row{
height: parent.height
width: parent.width - 10
anchors.horizontalCenter: parent.horizontalCenter
property real columnWidth: (width - 10) / 3
property int rad: 10
spacing: 5
Rectangle{
height: parent.height
width: parent.columnWidth
radius: parent.rad
color: "lightsteelblue"
Text{
anchors.centerIn: parent
text: form0
font.pointSize: del.fontSize
}
}
Rectangle{
height: parent.height
width: parent.columnWidth
radius: parent.rad
color: "lightsteelblue"
Text{
anchors.centerIn: parent
text: form1
font.pointSize: del.fontSize
}
}
Rectangle{
height: parent.height
width: parent.columnWidth
radius: parent.rad
color: "lightsteelblue"
Text{
anchors.centerIn: parent
text: form2
font.pointSize: del.fontSize
}
}
}
}
}

模型是:

#ifndef IRREGULARLISTWRAPPER_H
#define IRREGULARLISTWRAPPER_H

#include <QObject>
#include <QList>
#include <QAbstractListModel>
#include <QMap>
#include "IrregularVerb.h"
#include "AbstractIrregularList.h"

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

// ~IrregularListWrapper() { delete db; }
// QList<QObject*> getdb() const { return *db; }

QString getLangName() const { return langName; }
Q_INVOKABLE void changeLang(Language l) { beginResetModel(); db = 0; /*QList<IrregularVerb*>();*/ setLang(l); endResetModel(); }

static QMap<Language, QString> plugins;

signals:
void langChanged();
protected:
void setLang(Language);
//QList<IrregularVerb*> db;
QString langName;
AbstractIrregularList * db;

};

#endif // IRREGULARLISTWRAPPER_H


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

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

const QString pluginPath = "/opt/MeeIrregulars/share/lib%1.so";

plugins[English] = pluginPath.arg("english");
plugins[German] = pluginPath.arg("german");

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

}


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

回溯:

#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

谢谢。

最佳答案

这是所有权问题。 get 返回的元素归 JS 所有并自动销毁。参见 this answer获取更多信息。

关于c++ - QML ListView 和段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com