QAbstractListModel und QML zugriff auf properties

Alles rund um die Programmierung mit Qt
Antworten
ZSchneidi
Beiträge: 65
Registriert: 16. Juli 2009 13:30

QAbstractListModel und QML zugriff auf properties

Beitrag von ZSchneidi »

Hey Leute,
hab probleme damit ein QAbstractListModel mit meiner QML oberfläche zu verbinden.
Derzeit bin ich an dem Punkt dass ich die Rollen meines model nicht gesetzt bekomme.
Ich bekomme den Fehler: ‘setRoleNames’ was not declared in this scope
Ok steht ja in der doku dass die deprecated ist, aber wodurch wird die ersetzt ?
Wie soll man das jetzt bewerkstelligt bekommen ?

clipmodel.h

Code: Alles auswählen

#include <QObject>
#include <QAbstractListModel>
 
#include "global_define.h"
#include "elements/clipelement.h"
 
class ClipModel : public QAbstractListModel
{
    Q_OBJECT
public:
 
    enum ClipElementRoles { ObjectRole = Qt::UserRole+1 };
 
    explicit ClipModel(QObject *parent = 0);
 
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 
    void addClip(ClipElement *clip);
 
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
 
private:
    ClipList m_clips; //typedef QList<QObject*> ClipList;--
 
signals:
 
public slots:
 
};
clipmodel.cpp

Code: Alles auswählen

#include "clipmodel.h"
 
ClipModel::ClipModel(QObject *parent) :
    QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;
    roles[ObjectRole] = "object";
    setRoleNames(roles);
}
 
QVariant ClipModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
        return QVariant();
 
    if (index.row() >= m_clips.size() || index.row() < 0)
        return QVariant();
 
    if (role == Qt::DisplayRole) {
        return QVariant::fromValue(this->m_clips.at(index.row()));
    }
 
    if (role == ObjectRole)
        return QVariant::fromValue(this->m_clips.at(index.row()));
}
 
void ClipModel::addClip(ClipElement *clip)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_clips.append(clip);
    endInsertRows();
}
 
 int ClipModel::rowCount(const QModelIndex & /* parent */) const
{
    return m_clips.count();
}

Code: Alles auswählen

ClipModel *model = new ClipModel(this);
 
    ClipElement *temp = new ClipElement();
    temp->setClipID("foo");
    temp->setClipStatus("bar");
    model->addClip(temp);
    this->viewer.rootContext()->setContextProperty("clip_model", model);
Der QML delegate zu dem objekten im model:

Code: Alles auswählen

Component {
            id: clipDelegate
            Item {
                id:clipContainer
                width: parent.parent.width
                height: 50
                Rectangle {
                    id:clipBackground
                    anchors.fill: parent
                    color: "red"
                    border.color: "black"
                    Text {
                        id: clipText
                        anchors.fill: parent
                        text: ??? #how to get the properties of the ClipElement ?
                    }
                }
            }
        }
Wie kann ich jetzt vom delegate aus auf die properties der ClipElemente also der objekte in der liste zugreifen ?

Habt ihr mal ein vollständiges Tutorial dazu gefunden ? Die Doku ist da sehr lückenhaft was das angeht.

Danke für jede Hilfe.
Antworten