Qt QML C++作为自定义 ListView 模型的结构 QList
我的项目中拥有的是包含结构元素列表的二进制文件:
typedef struct {
unsigned int id;
char name[SIZE];
} Entry;
从文件读取数据后,我将所有读取的值存储在类的以下字段中:
QVector<Entry> entires;
我确实使用以下声明将此列表公开给 QML:
Q_PROPERTY(QVector<Entry> FileContents READ getEntries NOTIFY TmpFileUpdated)
其次是 getter 和 setter 方法。
inline QVector<Entry> getEntries ()
{
return this->entires;
}
inline void setEntries(QVector<entires> entries)
{
this->entires= entries;
emit TmpFileUpdated();
}
每次读取文件时,都会使用“setEntries”方法来设置向量并发出信号。
QML 中的列表视图将 Q_PROPERTY FileContents 附加到模型:
ListView {
id: myListView
width: 200
height: 400
model: myInjectedObject.FileContents
delegate: Row {
spacing: 10
Text {
text: model.entry_type // (1)
font.pixelSize: 12
horizontalAlignment: "AlignHCenter"
verticalAlignment: "AlignVCenter"
height: 20
}
}
}
如何访问结构列表中保存的数据并将其显示在 QML 中?
更新: 根据您的建议,我稍微更改了代码,现在可以正常编译了。创建了以下类:
class EntryClass: QObject
{
Q_OBJECT
Q_PROPERTY(QString entry_name READ getEntryName)
public:
inline EntryClass(Entry entrystruct)
{
this->entry = entrystruct;
}
private:
Entry entry;
inline QString getEntryName() const
{
return this->entry->entry_name;
}
};
ListView {
id: myListView
width: 200
height: 400
model: myInjectedObject.FileContents
delegate: Row {
spacing: 10
Text {
text: modelData.entry_name // (1)
font.pixelSize: 12
horizontalAlignment: "AlignHCenter"
verticalAlignment: "AlignVCenter"
height: 20
}
}
}
更新 2 好的,经过更多分析,我设法找到了可行的解决方案。关于上面的 ListView 声明,它已更新为当前状态(通过引用传递结构不起作用,我必须使用按值复制)。
这两个答案在某种程度上都有帮助,但由于只能接受一个,我将接受 Radon 编写的第一个答案。 谢谢两位指导!
What I have in my project is the binary file that contains list of structure elements:
typedef struct {
unsigned int id;
char name[SIZE];
} Entry;
After reading the data from file I have all the read values stored in the following field of my class:
QVector<Entry> entires;
I do expose this list to QML with the following declaration:
Q_PROPERTY(QVector<Entry> FileContents READ getEntries NOTIFY TmpFileUpdated)
followed by getter and setter methods.
inline QVector<Entry> getEntries ()
{
return this->entires;
}
inline void setEntries(QVector<entires> entries)
{
this->entires= entries;
emit TmpFileUpdated();
}
Each time the file is read "setEntries" method is used to set the vector and to emit the signal.
The listview in QML has the the Q_PROPERTY FileContents attached to the model:
ListView {
id: myListView
width: 200
height: 400
model: myInjectedObject.FileContents
delegate: Row {
spacing: 10
Text {
text: model.entry_type // (1)
font.pixelSize: 12
horizontalAlignment: "AlignHCenter"
verticalAlignment: "AlignVCenter"
height: 20
}
}
}
How to access the data that is kept on the list of structures and display it in QML?
UPDATE:
After your suggestions I changed the code slightly and it compiles fine now. A following class was created:
class EntryClass: QObject
{
Q_OBJECT
Q_PROPERTY(QString entry_name READ getEntryName)
public:
inline EntryClass(Entry entrystruct)
{
this->entry = entrystruct;
}
private:
Entry entry;
inline QString getEntryName() const
{
return this->entry->entry_name;
}
};
ListView {
id: myListView
width: 200
height: 400
model: myInjectedObject.FileContents
delegate: Row {
spacing: 10
Text {
text: modelData.entry_name // (1)
font.pixelSize: 12
horizontalAlignment: "AlignHCenter"
verticalAlignment: "AlignVCenter"
height: 20
}
}
}
UPDATE 2
OK, after some more analysis I've managed to find the solution that works. Regarding the ListView declaration above it was updated to the current state (passing struct by reference didn't work, I had to use copy by value).
Both answers were helpful in some way, but since only one can be accepted I'll accept the first one written by Radon.
Thank you both for guidance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QML 无法访问“低级”
struct
类型。但是您可以创建一个继承
QObject< 的类
EntryClass
/code>并将
id
和name
添加为 Qt属性(内部EntryClass
可以使用相应的Entry
结构实例的数据,例如通过使用指向它的指针)。然后您应该能够在 QML 中访问这些属性。(不过我没测试过)
QML cannot access "low-level"
struct
types.But you can create a class
EntryClass
that inheritsQObject
and addid
andname
as Qt properties (internallyEntryClass
can use the data of the correspondingEntry
struct instances, e.g. by using a pointer to it). Then you should be able to access these properties in QML.(But I didn't test it)
Radon 是对的,导出到 QML 的对象需要属性(或 Q_INVOKABLE 函数)。
此外,QVector 也不起作用。您需要使用 QDeclarativeListProperty 或 QVariantList 作为属性类型。
Radon is right, your objects exported to QML need properties (or Q_INVOKABLE functions).
Additionally, QVector won't work either. You need to use QDeclarativeListProperty or QVariantList as the property type.