Qt QML C ++ Q结构列表作为自定义ListView模型 [英] Qt QML C++ QList of structs as custom ListView model

查看:553
本文介绍了Qt QML C ++ Q结构列表作为自定义ListView模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个二进制文件,其中包含结构元素列表:

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;

我确实通过以下声明将此列表公开给QML:

I do expose this list to QML with the following declaration:

Q_PROPERTY(QVector<Entry> FileContents READ getEntries NOTIFY TmpFileUpdated)

接着是getter和setter方法.

followed by getter and setter methods.

inline QVector<Entry> getEntries () 
{ 
    return this->entires; 
}

inline void setEntries(QVector<entires> entries) 
{ 
    this->entires= entries; 
    emit TmpFileUpdated(); 
}

每次读取文件时,都会使用"setEntries"方法设置矢量并发出信号.

Each time the file is read "setEntries" method is used to set the vector and to emit the signal.

QML中的列表视图具有附加到模型的Q_PROPERTY FileContents:

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

如何访问保留在结构列表中的数据并将其显示在QML中?

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

更新2 好的,经过更多分析,我设法找到了可行的解决方案.关于上面的ListView声明,它已更新为当前状态(按引用传递结构无效,我必须使用按值复制).

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).

两个答案都在某种程度上有所帮助,但是由于只能接受一个答案,因此我将接受Radon撰写的第一个答案. 谢谢你们的指导!

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!

推荐答案

QML无法访问低级" struct类型.

QML cannot access "low-level" struct types.

但是您可以创建一个继承 QObject 的类EntryClass.并将idname添加为 Qt属性(内部EntryClass可以使用相应的Entry结构实例的数据,例如通过使用指向它的指针).然后,您应该能够在QML中访问这些属性.

But you can create a class EntryClass that inherits QObject and add id and name as Qt properties (internally EntryClass can use the data of the corresponding Entry struct instances, e.g. by using a pointer to it). Then you should be able to access these properties in QML.

(但是我没有测试)

这篇关于Qt QML C ++ Q结构列表作为自定义ListView模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆