从 QAbstractListModel 返回自定义 QObject 子类并在 ListView 中使用它
returning a custom QObject subclass from QAbstractListModel and using it in a ListView
如何 return 来自 QAbstractListModel 的自定义 QObject 子 class 并在 QML ListView 中使用它。
我尝试 return 对象作为显示角色,我在我的 qml display.property 中使用它来访问属性,它工作正常但我在一些帖子上看到人们使用模型作为 qml 中的 qobject并以 model.property 的形式访问属性。我错过了什么吗?
另一个问题:如果我想在 ListView 级别公开对象并使用它来设置一些其他面板,如主视图详细信息,则将角色(在我的例子中是显示)公开为变体 属性 在委托中使用 onCurrentItemChanged 信号将其设置在列表视图级别是正确的方法吗??
这是我正在尝试的方法,但它不起作用:
#ifndef NOTE_H
#define NOTE_H
#include <QObject>
class Note : public QObject
{
Q_OBJECT
Q_PROPERTY(QString note READ note WRITE setNote NOTIFY noteChanged)
Q_PROPERTY(int id READ id WRITE setId NOTIFY idChanged)
QString m_note;
int m_id;
public:
explicit Note(QObject *parent = 0);
Note(QString note, int id, QObject *parent = 0);
QString note() const
{
return m_note;
}
int id() const
{
return m_id;
}
signals:
void noteChanged(QString note);
void idChanged(int id);
public slots:
void setNote(QString note)
{
if (m_note == note)
return;
m_note = note;
emit noteChanged(note);
}
void setId(int id)
{
if (m_id == id)
return;
m_id = id;
emit idChanged(id);
}
};
#endif // NOTE_H
视图模型:
#ifndef NOTESVIEWMODEL_H
#define NOTESVIEWMODEL_H
#include <QAbstractListModel>
#include <QVector>
#include "note.h"
class NotesViewModel : public QAbstractListModel
{
Q_OBJECT
QVector<Note*> notes;
public:
NotesViewModel();
QVariant data(const QModelIndex &index, int role) const override;
int rowCount(const QModelIndex &parent) const override;
};
#endif // NOTESVIEWMODEL_H
视图模型的实现:
NotesViewModel::NotesViewModel()
{
notes.append(new Note("note 1", 1));
notes.append(new Note("note 2", 2));
notes.append(new Note("note 3", 3));
notes.append(new Note("note 4", 4));
notes.append(new Note("note 5", 5));
}
QVariant NotesViewModel::data(const QModelIndex &index, int role) const
{
qDebug() << "fetching data : " << index.row();
if(!index.isValid()) return QVariant();
if(index.row() >= 5) return QVariant();
if(role == Qt::DisplayRole)
return QVariant::fromValue(notes[index.row()]);
return QVariant();
}
int NotesViewModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return notes.count();
}
我玩了一下 qml ListView,试图弄清楚如何将 currentItem 数据公开给外界。我是这样弄的,也许对我这样的新手有用
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
id: modell
ListElement {
fname: "houssem"
age: 26
}
ListElement {
fname: "Anna"
age: 26
}
ListElement {
fname: "Nicole"
age: 26
}
ListElement {
fname: "Adam"
age: 27
}
}
ListView {
id: lv
height: 100
width: 200
clip: true
model: modell
property string selectedName: currentItem.name
delegate: Component {
Item {
id: mainItem
width: ListView.view.width
height: 80
property string name: fname
Text {
text: "name " + fname + " age " + age
}
MouseArea {
anchors.fill: parent
onClicked: mainItem.ListView.view.currentIndex = index
}
}
}
}
Text {
anchors.right: parent.right
text: lv.selectedName
}
}
正如您在代码中看到的那样,为了公开 currentItem 的数据,我只需要在委托 Item 中声明属性(在本示例中为字符串类型的名称 属性),然后绑定一个 [= ListView 上的 selectedName(本例中为 selectedName)到 currentItem.property,这样当我 select 来自列表,我可以从 UI.
的其他项目中访问该项目
既然你已经回答了你的第二个问题,让我来回答第一个问题,即 display.propertyName
与 model.propertyName
基本上第一个只是 model.display.propertyName
的缩写,即正在访问给定索引处模型数据的 "display" 属性。在您的情况下,returns 一个对象,其上有属性。
model.propertyName
也可以写成 propertyName
,这意味着调用模型的 data() 方法时 "role" 是 "propertyName" 的等效数值。
从 "propertyName" 到其等效数值的映射是使用 QAbstractItemModel::roleNames()
方法完成的。
它的默认实现有一些基本映射,例如映射 "display" 到 Qt::DisplayRole
.
如何 return 来自 QAbstractListModel 的自定义 QObject 子 class 并在 QML ListView 中使用它。
我尝试 return 对象作为显示角色,我在我的 qml display.property 中使用它来访问属性,它工作正常但我在一些帖子上看到人们使用模型作为 qml 中的 qobject并以 model.property 的形式访问属性。我错过了什么吗?
另一个问题:如果我想在 ListView 级别公开对象并使用它来设置一些其他面板,如主视图详细信息,则将角色(在我的例子中是显示)公开为变体 属性 在委托中使用 onCurrentItemChanged 信号将其设置在列表视图级别是正确的方法吗??
这是我正在尝试的方法,但它不起作用:
#ifndef NOTE_H
#define NOTE_H
#include <QObject>
class Note : public QObject
{
Q_OBJECT
Q_PROPERTY(QString note READ note WRITE setNote NOTIFY noteChanged)
Q_PROPERTY(int id READ id WRITE setId NOTIFY idChanged)
QString m_note;
int m_id;
public:
explicit Note(QObject *parent = 0);
Note(QString note, int id, QObject *parent = 0);
QString note() const
{
return m_note;
}
int id() const
{
return m_id;
}
signals:
void noteChanged(QString note);
void idChanged(int id);
public slots:
void setNote(QString note)
{
if (m_note == note)
return;
m_note = note;
emit noteChanged(note);
}
void setId(int id)
{
if (m_id == id)
return;
m_id = id;
emit idChanged(id);
}
};
#endif // NOTE_H
视图模型:
#ifndef NOTESVIEWMODEL_H
#define NOTESVIEWMODEL_H
#include <QAbstractListModel>
#include <QVector>
#include "note.h"
class NotesViewModel : public QAbstractListModel
{
Q_OBJECT
QVector<Note*> notes;
public:
NotesViewModel();
QVariant data(const QModelIndex &index, int role) const override;
int rowCount(const QModelIndex &parent) const override;
};
#endif // NOTESVIEWMODEL_H
视图模型的实现:
NotesViewModel::NotesViewModel()
{
notes.append(new Note("note 1", 1));
notes.append(new Note("note 2", 2));
notes.append(new Note("note 3", 3));
notes.append(new Note("note 4", 4));
notes.append(new Note("note 5", 5));
}
QVariant NotesViewModel::data(const QModelIndex &index, int role) const
{
qDebug() << "fetching data : " << index.row();
if(!index.isValid()) return QVariant();
if(index.row() >= 5) return QVariant();
if(role == Qt::DisplayRole)
return QVariant::fromValue(notes[index.row()]);
return QVariant();
}
int NotesViewModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return notes.count();
}
我玩了一下 qml ListView,试图弄清楚如何将 currentItem 数据公开给外界。我是这样弄的,也许对我这样的新手有用
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
id: modell
ListElement {
fname: "houssem"
age: 26
}
ListElement {
fname: "Anna"
age: 26
}
ListElement {
fname: "Nicole"
age: 26
}
ListElement {
fname: "Adam"
age: 27
}
}
ListView {
id: lv
height: 100
width: 200
clip: true
model: modell
property string selectedName: currentItem.name
delegate: Component {
Item {
id: mainItem
width: ListView.view.width
height: 80
property string name: fname
Text {
text: "name " + fname + " age " + age
}
MouseArea {
anchors.fill: parent
onClicked: mainItem.ListView.view.currentIndex = index
}
}
}
}
Text {
anchors.right: parent.right
text: lv.selectedName
}
}
正如您在代码中看到的那样,为了公开 currentItem 的数据,我只需要在委托 Item 中声明属性(在本示例中为字符串类型的名称 属性),然后绑定一个 [= ListView 上的 selectedName(本例中为 selectedName)到 currentItem.property,这样当我 select 来自列表,我可以从 UI.
的其他项目中访问该项目既然你已经回答了你的第二个问题,让我来回答第一个问题,即 display.propertyName
与 model.propertyName
基本上第一个只是 model.display.propertyName
的缩写,即正在访问给定索引处模型数据的 "display" 属性。在您的情况下,returns 一个对象,其上有属性。
model.propertyName
也可以写成 propertyName
,这意味着调用模型的 data() 方法时 "role" 是 "propertyName" 的等效数值。
从 "propertyName" 到其等效数值的映射是使用 QAbstractItemModel::roleNames()
方法完成的。
它的默认实现有一些基本映射,例如映射 "display" 到 Qt::DisplayRole
.